【发布时间】:2015-06-23 13:39:27
【问题描述】:
我有一个 IIS 托管的 MVC 5 应用程序,它使用 Asp.Net Identity 和 OWIN 通过 .AspNet.ApplicationCookie 进行身份验证。从它的一个观点来看,我通过 SignalR JS 客户端在自托管 SignalR 集线器(在同一台服务器上运行)上调用长时间运行的方法。这些调用都按预期工作。我现在希望用 [Authorize(Roles = "Administrator")] 来装饰我的集线器。这已被证明是有问题的。在集线器方法中设置断点会显示 Context.User 为空,即使 .AspNet.ApplicationCookie 清楚地位于 Context.RequestCookies 中。
这是集线器的引导程序(在 Windows 服务中自托管):
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
map.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
});
var hubConfiguration = new HubConfiguration();
map.RunSignalR(hubConfiguration);
});
这是网络应用程序的身份验证配置(托管在 IIS 中):
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(UserAccountContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
问题 1: 在上述场景中是否可以使用 [Authorize]?如果有,怎么做?
问题 2: 将自托管集线器合并到 IIS 托管应用程序会更好吗?如果是这样,在 IIS 下长时间运行的集线器方法是否存在任何问题?
更新 1
我尝试将 TicketDataFormat = new TicketDataFormat(new MachineKeyDataProtector("ASP.NET Identity")) 添加到集线器配置上的 CookieAuthenticationOptions 中,但这没有帮助。当然看起来这应该比现在更容易。
【问题讨论】:
-
代码将帮助我们帮助您。 SignalR 集线器的引导程序是什么样的?
-
托管在同一台服务器上,但在同一个应用程序中?因为我认为默认情况下 Asp.net Identity 只为同一个应用程序而不是服务器共享凭据
-
@bto.rdz 集线器作为 Windows 服务自托管,因此它与 IIS 中托管的 MVC 5 应用程序不在同一个应用程序中。
-
@LanceHeld 你需要的是分享授权,试试这个链接stackoverflow.com/questions/20589429/…
-
@bto.rdz 您发布的链接是我想要的,但没有提供我已经尝试过的任何新信息。
标签: asp.net-mvc asp.net-mvc-5 signalr authorize self-hosting