加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何自定义UseExternalSignInCookie?

发布时间:2020-12-16 07:25:04 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET Identity 2.0并尝试将“.AspNet.ExternalCookie”cookie的域设置为“.mydomain.com”,因为我想从另一个子域读取cookie. 一些解决方案说我可以更改此代码: app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 对此
我正在使用ASP.NET Identity 2.0并尝试将“.AspNet.ExternalCookie”cookie的域设置为“.mydomain.com”,因为我想从另一个子域读取cookie.

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

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,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,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();
    }

解决方法

似乎有两个解决方案:

解决方案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,CookieDomain = ".mydomain.com"
        });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读