【问题标题】:Enabling Authentication with SIgnalR against Azure Mobile Services when debugging on localhost在本地主机上调试时启用对 Azure 移动服务的 SIGnalR 身份验证
【发布时间】:2016-10-14 00:36:50
【问题描述】:

这类似于Enabling Authentication with SIgnalR against Azure Mobile Services and a Javascript client,但有一点不同。我有一个 Zumo 身份验证的应用程序,它具有我想在本地调试的 SignalR。对于客户,

public App()
{
#if DEBUG            
  var client = new MobileServiceClient(debugUrl);
  client.AlternateLoginHost = new Uri(releaseUrl);
#else
  var client = new MobileServiceClient(mobileAppReleaseUrl);
#endif
  var hub = new HubConnection(client.MobileAppUri.AbsoluteUri);
..
}

为了验证 SignalR,我正在使用

conn.Headers["x-zumo-auth"] = _appService.CurrentUser.MobileServiceAuthenticationToken;

我使用的服务器端

public class ZumoUserIdProvider : IUserIdProvider
{
    public string GetUserId(IRequest request)
    {
        if (request == null)
        {
            throw new ArgumentNullException("request");
        }

        if (request.User != null && request.User.Identity != null)
        {
            var identity = (ClaimsIdentity)request.User.Identity;
            var identifier = identity.FindFirst(ClaimTypes.NameIdentifier);
            if (identifier != null)
            {
                return identifier.Value;
            }
        }

        return null;
    }
}

随着

GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new ZumoUserIdProvider());

令人惊讶的是,这在发布到 Azure 时确实有效,尽管我有点担心 Web 套接字(也许我需要将令牌填充到查询字符串中)。现在的问题是,在 localhost 中调试时,虽然我能够进行身份验证(社交登录),但当我尝试在 localhost 端使用 SignalR 时,我总是无法通过身份验证。我怎样才能让它发挥作用?

更新。 我认为曾经有 SignalRExtensionConfig.Initialize(); 虽然我在当前版本的 Azure 移动服务中看不到它。这可以在 WindowsAzure.MobileServices.Backend.SignalR 中找到,但它确实具有看起来不容易实现的确切依赖关系,例如,AspNet.Cors = 5.2.2,我有 5.2.3。

Update2. 显然有 https://msdn.microsoft.com/en-us/library/azure/mt587593.aspx

SignalRExtensionConfig 类

SignalRExtensionConfig 类提供特定于 SignalR 的配置。 命名空间:Microsoft.Azure.Mobile.Server.Config 程序集:Microsoft.Azure.Mobile.Server.SignalR(在 Microsoft.Azure.Mobile.Server.SignalR.dll 中)

但我在 nuget 中看不到这一点。我只是在使用常规的 AspNet SignalR 库。我的设置看起来像

   public static void ConfigureMobileApp(IAppBuilder app)
    {            
        HttpConfiguration config = new HttpConfiguration();

        new MobileAppConfiguration()
            .UseDefaultConfiguration()
            .ApplyTo(config);

        // Use Entity Framework Code First to create database tables based on your DbContext
        //            Database.SetInitializer(new MobileServiceInitializer());
        var migrator = new DbMigrator(new Migrations.Configuration());
        migrator.Update();

        MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

        // http://blogs.perficient.com/microsoft/2016/05/how-to-add-custom-claims-to-azure-mobile-app-authentication/
        if (string.IsNullOrEmpty(settings.HostName))
        {
            app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
            {
                // This middleware is intended to be used locally for debugging. By default, HostName will
                // only have a value when running in an App Service application.
                SigningKey = ConfigurationManager.AppSettings["SigningKey"],
                ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
                ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
                TokenHandler = config.GetAppServiceTokenHandler()
            });
        }

        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            //map.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
            //{
            //    Provider = new ZumoOAuthBearerProvider()
            //});
            var hubConfig = new HubConfiguration
            {
                Resolver = GlobalHost.DependencyResolver
            };
            map.RunSignalR(hubConfig);
        });

        app.UseWebApi(config);

    }

【问题讨论】:

    标签: asp.net azure authentication


    【解决方案1】:

    我可以看到你正在使用Azure Mobile App Service

    我会说问题是您在本地运行应用服务时无法正确社交登录。 您在启用社交身份验证时在社交提供者上指定的回调 URI 设置为您的移动应用服务 URI 而不是 localhost,这就是您没有将身份验证标头作为 x-zumo-auth 返回的原因。

    一种解决方法是启用Remote Debugging,将调试版本部署到您的Mobile App Service 并远程调试它。

    在此处查看更多信息:Troubleshoot a web app in Azure App Service using Visual Studio

    【讨论】:

    • 虽然这不是我所希望的答案,但这个解决方案在过渡期间确实有效,尽管我最终将半生不熟的代码发布到 Azure,这让我很烦恼。
    【解决方案2】:

    您正在学习 Azure 移动服务的教程,但仍在使用 Azure 移动应用。两者不兼容。

    明确地说,我们没有在最新的 SDK 中包含 SignalR - 这是您必须自己实现的东西。正确设置身份验证后,任何控制器上的简单 [Authorize] 标签将确保用户通过身份验证。

    SignalR 设置现在与 Azure 移动应用正交。

    【讨论】:

    • 确实是。虽然我认为如何验证 localhost SignalR 调用仍然是一个悬而未决的问题。
    • 您可以在本地运行,但可以远程验证。将客户端上的 AlternateLoginUri 设置为您的 azurewebsites.net 端点。
    • 即使设置了 AlternateLoginUri,SignalR 也不会进行身份验证。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2013-06-15
    • 2018-08-12
    相关资源
    最近更新 更多