Asp.net 在本地主机上调试时启用针对Azure移动服务的SIgnalR身份验证
这与类似,但有一点扭曲。我有一个Zumo认证的应用程序,它有我想在本地调试的信号器。对于客户而言Asp.net 在本地主机上调试时启用针对Azure移动服务的SIgnalR身份验证,asp.net,azure,authentication,Asp.net,Azure,Authentication,这与类似,但有一点扭曲。我有一个Zumo认证的应用程序,它有我想在本地调试的信号器。对于客户而言 public App() { #if DEBUG var client = new MobileServiceClient(debugUrl); client.AlternateLoginHost = new Uri(releaseUrl); #else var client = new MobileServiceClient(mobileAppReleaseUr
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时,我总是未经身份验证。我怎样才能让它工作
更新。我想以前有SignalRExtensionConfig.Initialize();
尽管我在Azure移动服务的当前版本中没有看到它。这可以在WindowsAzure.MobileServices.Backend.SignalR中找到,但它确实具有看起来不容易实现的精确依赖关系,例如,AspNet.Cors=5.2.2,我有5.2.3
更新2。显然有
SignalRExtensionConfig类
SignalRExtensionConfig类提供特定于SignalR的配置。
命名空间:Microsoft.Azure.Mobile.Server.Config
程序集:Microsoft.Azure.Mobile.Server.SignalR(在Microsoft.Azure.Mobile.Server.SignalR.dll中)
但我在努吉看不到这一点。我正在使用常规的AspNet信号器库。我的设置看起来像
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移动应用程序服务
我想说的是,问题在于,在本地运行应用程序服务时,您无法正确登录社交网站。
启用社交身份验证时,您在社交提供商上指定的回调URI被设置为您的移动应用程序服务URI,而不是本地主机,这就是为什么您没有将身份验证头设置为x-zumo-auth
一种解决方法是启用远程调试
,将调试构建部署到移动应用程序服务
,并对其进行远程调试
请参阅此处的更多信息:您正在学习Azure移动服务教程,但仍在使用Azure移动应用程序。这两者不相容 明确地说,我们没有在最新的SDK中包含Signaler—这是您必须自己实现的。正确设置身份验证后,任何控制器上的一个简单的
[Authorize]
标记都将确保用户已通过身份验证
SignalR设置现在与Azure移动应用程序是正交的。虽然这不是我所希望的答案,但这个解决方案在过渡期间确实有效,尽管我最终向Azure发布了半生不熟的代码,这让我很烦。事实上,我是。尽管我认为如何对本地主机信号器调用进行身份验证仍然是一个悬而未决的问题。您可以在本地运行,但可以远程进行身份验证。将客户端上的AlternateLoginUri设置为azurewebsites.net端点。即使设置了AlternateLoginUri,Signal也不会进行身份验证。