2014-05-19 14 views
4

我正在使用ASP.NET身份2.0,并试图将“.AspNet.ExternalCookie”cookie的域设置为“.mydomain.com”,因为我想从另一个子域读取该cookie。我如何定制UseExternalSignInCookie?

一些解决方案,说我可以改变这个代码:

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

要这样:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = DefaultAuthenticationTypes.ExternalCookie, 
    CookieName = CookieAuthenticationDefaults.CookiePrefix + "External", 
    LoginPath = new PathString("/Account/Login"), 
    CookieDomain = ".mydomain.com" 
}); 

但我收到以下错误:

A default value for SignInAsAuthenticationType was not found in IAppBuilder Properties. This can happen if your authentication middleware are added in the wrong order, or if one is missing.

我的全代码如下像这样:

 public void ConfigureAuth(IAppBuilder app) 
    { 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      Provider = new CookieAuthenticationProvider 
      { 
       OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
        validateInterval: TimeSpan.FromMinutes(30), 
        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
      } 
     }); 

     //app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ExternalCookie, 
      CookieName = CookieAuthenticationDefaults.CookiePrefix + "External", 
      LoginPath = new PathString("/Account/Login"), 
      CookieDomain = ".mydomain.com", 
      ExpireTimeSpan = TimeSpan.FromMinutes(5) 
     }); 

     app.UseMicrosoftAccountAuthentication(
      clientId: "1", 
      clientSecret: "1"); 

     app.UseTwitterAuthentication(
      consumerKey: "2", 
      consumerSecret: "2"); 

     app.UseFacebookAuthentication(
      appId: "3", 
      appSecret: "3"); 

     app.UseGoogleAuthentication(); 
    } 

回答

10

似乎有此2个解决方案:

解决方案1:

添加

using Microsoft.Owin.Security; 

添加

app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie); 

之前app.UseCookieAuthentication(...)

解决方案N 2:

添加

app.Properties["Microsoft.Owin.Security.Constants.DefaultSignInAsAuthenticationType"] = "ExternalCookie"; 

之前app.UseCookieAuthentication(...)

而且AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive应该为了不自动登录用户,如果他来自外部供应商认证加入(它应该由应用程序控制,并且他只应通过ApplicationCookie进行身份验证)。

 app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie); 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ExternalCookie, 
      AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive, 
      LoginPath = new PathString("/accounts/signin"), 
      CookieHttpOnly = true, 
      CookieName = CookieAuthenticationDefaults.CookiePrefix + "External", 
      CookieDomain = ".mydomain.com" 
     });