Owin web角色不会解密Owin MVC Application提供的cookie

时间:2015-10-08 11:05:44

标签: authentication cookies owin

MVC Web应用程序

我有一个以前使用过FormsAuthentication的MVC 5网站,我已经切换到OWIN身份验证。使用OWIN登录和退出网站可以正常工作。这是启动配置。

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/LogOn")
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }

登录时有一些基本的声明设置...

    public void SignIn(string username, bool remember, string userData)
    {
        var claims = new List<Claim>();

        var user = JsonConvert.DeserializeObject<VastIdentity>(userData);
        // create *required* claims
        claims.Add(new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()));
        claims.Add(new Claim(ClaimTypes.Name, username));

        // custom – serialised user state
        claims.Add(new Claim("userState", userData));

        var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

        AuthenticationManager.SignIn(new AuthenticationProperties()
        {
            AllowRefresh = true,
            IsPersistent = remember,
            ExpiresUtc = DateTime.UtcNow.AddDays(7)
        }, identity);
    }

一切正常,用户可以使用适当的cookie登录和退出MVC应用程序。

SignalR Web角色

我有一个独立的天蓝色网络角色,主持信号器。它有以下启动...

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new CustomUserIdProvider());

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            AuthenticationMode = AuthenticationMode.Active,
            CookieName = ".AspNet.ApplicationCookie",
            Provider = new CustomAuthProvider()
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Branch the pipeline here for requests that start with "/signalr"
        app.Map("/signalr", map =>
        {
            // Setup the CORS middleware to run before SignalR.
            // By default this will allow all origins. You can 
            // configure the set of origins and/or http verbs by
            // providing a cors options with a different policy.
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                // You can enable JSONP by uncommenting line below.
                // JSONP requests are insecure but some older browsers (and some
                // versions of IE) require JSONP to work cross domain
                // EnableJSONP = true
            };
            // Run the SignalR pipeline. We're not using MapSignalR
            // since this branch already runs under the "/signalr"
            // path.
            map.RunSignalR(hubConfiguration);
        });
    }
}

问题1:当与SignalR集线器建立连接并且调用OnConnected时,Context.User.Identity是一个空的GenericPrincipal并且不包含来自网站cookie的声明。这就是我打算实现自定义身份验证提供程序并自行设置身份的原因。但它似乎没有被使用。

public class NotificationHub : Hub
{
    // snip

    public override Task OnConnected()
    {
        string name = Context.User.Identity.Name;

        _Connections.Add(name, Context.ConnectionId);

        return base.OnConnected();
    }
  }

当我检查上下文中的cookie时,我可以看到加密的 .AspNet.ApplicationCookie 正在与请求一起传递。但我无法弄清楚如何让OWIN解密并使用可用的声明。

问题2:自定义身份验证提供程序永远不会运行。当我在其中设置断点时,它永远不会进入任何一种方法。

public class CustomAuthProvider : CookieAuthenticationProvider
{
    public override void ResponseSignIn(CookieResponseSignInContext context)
    {
        // do some custom stuff here
        base.ResponseSignIn(context);
    }

    public override Task ValidateIdentity(CookieValidateIdentityContext context)
    {
        // do some custom stuff here
        return base.ValidateIdentity(context);
    }
}

我对OWIN身份验证没有太多经验。任何人都可以看到信号器Web角色cookie身份验证可能出现的问题吗?

1 个答案:

答案 0 :(得分:0)

拔掉头发后,我重新检查了owin nugget软件包的版本,注意到Web应用程序使用的是3.0.1,而SignalR Web角色使用的是2.x.

解决方案是确保owin库在应用程序之间的版本相同。看起来owin无法解密从不同版本生成的cookie。

修改:由于解密Cookie的问题与版本不匹配有关,因此不需要自定义身份验证提供程序和Cookie名称。

这导致以下简单的owin初始化为web角色......

if(AdMob) AdMob.prepareInterstitial( {adId:admobid.interstitial, autoShow:false} ); 
相关问题