如何自定义UseExternalSignInCookie?

Bas*_*ouk 4 asp.net owin asp.net-mvc-5 asp.net-identity-2

我正在使用ASP.NET Identity 2.0并尝试将".AspNet.ExternalCookie"cookie的域设置为".mydomain.com",因为我想从另一个子域读取cookie.

一些解决方案说我可以更改此代码:

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)

对此:

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

但是我收到以下错误:

在IAppBuilder属性中找不到SignInAsAuthenticationType的默认值.如果您的身份验证中间件以错误的顺序添加,或者如果缺少一个,则会发生这种情况.

我的完整代码如下所示:

        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();
    }
Run Code Online (Sandbox Code Playgroud)

Bas*_*ouk 9

似乎有两个解决方案:

解决方案1:

using Microsoft.Owin.Security;
Run Code Online (Sandbox Code Playgroud)

app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)

在app.UseCookieAuthentication(...)之前

解决方案2:

app.Properties["Microsoft.Owin.Security.Constants.DefaultSignInAsAuthenticationType"] = "ExternalCookie";
Run Code Online (Sandbox Code Playgroud)

在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"
        });
Run Code Online (Sandbox Code Playgroud)