【发布时间】: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