我正在使用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();
}