How can I customize UseExternalSignInCookie?

I am using ASP.NET Identity 2.0 and am trying to set the cookie .mydomain.com for the domain .AspNet.ExternalCookie because I want to read a cookie from another subdomain.

Some solutions say that I can change this code:

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

For this:

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

But I get the following error:

The default value for the SignInAsAuthenticationType parameter was not found in the IAppBuilder properties. This can happen if your middleware for authentication is added in the wrong order or missing.

My complete code is as follows:

        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();
    }
+4
source share
1 answer

, 2 :

1:

using Microsoft.Owin.Security;

app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);

app.UseCookieAuthentication(...)

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"
        });
+10

Source: https://habr.com/ru/post/1541082/


All Articles