在本地主机上调试时对SINUMALR启用针对Azure移动服务的身份验证 [英] Enabling Authentication with SIgnalR against Azure Mobile Services when debugging on localhost

查看:80
本文介绍了在本地主机上调试时对SINUMALR启用针对Azure移动服务的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于通过以下方式启用身份验证针对Azure移动服务和Javascript客户端的SIgnalR ,但有所不同.我有一个经Zumo认证的应用,该应用具有我想在本地调试的SignalR.对于客户,

this is similar to Enabling Authentication with SIgnalR against Azure Mobile Services and a Javascript client but with a twist. I have a Zumo-authenticated app that has SignalR that I would like to locally debug. For the client,

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);
..
}

我正在使用

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时,我始终会未经身份验证.我该如何运作?

Amazingly, this actually works when published to Azure, though I'm a little bit worried about Web Sockets (maybe I need to stuff the token into the query string). The problem now is that when debugging in localhost, although I am able to authenticate (social login), I always get unauthenticated when I try to use SignalR on the localhost side. How can I make it work?

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

Update. i think there used to be SignalRExtensionConfig.Initialize(); although I don't see it in the current version of Azure Mobile Services. This is found in WindowsAzure.MobileServices.Backend.SignalR but it has really exact dependencies that do not look easy to fulfill, e.g., AspNet.Cors = 5.2.2, I have 5.2.3.

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

SignalRExtensionConfig类

SignalRExtensionConfig Class

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

The SignalRExtensionConfig class provides configuration specific to SignalR. Namespace: Microsoft.Azure.Mobile.Server.Config Assembly: Microsoft.Azure.Mobile.Server.SignalR (in Microsoft.Azure.Mobile.Server.SignalR.dll)

但是我在nuget中看不到它.我只是使用常规的AspNet SignalR库.我的设置看起来像

but I do not see this in nuget. I'm just using the regular AspNet SignalR library. My Setup looks like

   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);

    }

推荐答案

如我所见,您正在使用Azure Mobile App Service.

As I can see you are using Azure Mobile App Service.

我要说的问题是,在本地运行应用程序服务时,您无法正确社交登录. 启用社交身份验证时,您在社交提供程序上指定的回调URI设置为移动应用程序服务URI(而不是localhost),这就是为什么您不获取身份验证标头为x-zumo-auth的原因.

I would say that the problem is that you can't social log in correctly when running the app service locally. The callback URIs you specified on the social provider when enabling the social authentication, are set to your mobile app service URI instead of localhost, that's why you are not getting the authentication headers as x-zumo-auth back.

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

A workaround would be to enable Remote Debugging, deploy a debug build to your Mobile App Service and remotely debug it.

在此处查看更多信息:问题排查使用Visual Studio在Azure App Service中创建一个Web应用程序

See more here: Troubleshoot a web app in Azure App Service using Visual Studio

这篇关于在本地主机上调试时对SINUMALR启用针对Azure移动服务的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆