【问题标题】:Store Claims into COOKIE in ASP.NET CORE 2.1在 ASP.NET CORE 2.1 中将声明存储到 COOKIE
【发布时间】:2019-05-14 19:38:05
【问题描述】:

我需要在身份验证成功后使用 OpenID 连接从身份提供者对用户进行身份验证,我需要将用户的声明存储到 cookie 中。

但我在 Request.Cookies 中没有找到接受三个参数的 append 方法,我只能将 keyvaluepair 作为signle 参数传递。

这是我的代码

public IActionResult Login(string provider, string returnUrl = null)
        {
             Challenge(new AuthenticationProperties { RedirectUri = returnUrl ?? "/" }, provider);
            Request.Cookies.Append("key", "value", new CookieOptions());

            return View();

 }

我还需要确认COOKIE存储的代码应该写在哪里,这样一旦认证成功就不能再次存储了。

这是我的身份验证代码

services.AddAuthentication(options =>
            {
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;

            }).AddOpenIdConnect(options =>
                {
                    options.Authority = "https://accounts.google.com";
                    options.ClientId = _clientID;
                    options.ResponseType = "code id_token";
                    options.Scope.Add("openid");
                    options.Scope.Add("profile");
                    options.Scope.Add("email");
                    options.SaveTokens = true;
                    options.ClientSecret = _clientSecret;
                    //options.GetClaimsFromUserInfoEndpoint = true;
                    options.CallbackPath = "/home/index";
                    //options.SignedOutRedirectUri = redirectUrl;

                            options.Events = new OpenIdConnectEvents()
                            {
                                // handle the logout redirection 
                                OnRedirectToIdentityProviderForSignOut = context =>
                                {
                                    context.Response.Redirect(redirectUrl);
                                    context.HandleResponse();

                                    return Task.CompletedTask;
                                }
                            };


                        }).AddCookie(options => {
                            options.LoginPath = "/auth/signin";

                        });

提前致谢。

【问题讨论】:

    标签: openid-connect asp.net-core-2.1 httpcookie


    【解决方案1】:

    不要自己将声明存储在 cookie 中,因为最终用户可以修改 cookie。如果我修改 cookie 并添加声明 Role, Administrator(或您的应用程序中表示访问权限的任何概念)会怎样。

    使用提供的 cookie 处理程序在身份验证后存储声明。这就是该模型的用途。 cookie 处理程序将对内容进行加密和签名以防止其被篡改。

    编辑后更新

    感谢DefaultSigninScheme,使用您的代码,您已经将身份验证结果存储在 Cookie 中。

    您应该在后续请求中提供的User 中提供结果声明。

    【讨论】:

    • 如何使用 cookie 处理程序来存储声明?我的老板让我在 cookie 中存储姓名和电子邮件声明,我不知道他为什么需要这样,所以我必须以任何可能的方式这样做
    • cookie 处理程序是正常的处理方式,请查看可用的文档/示例。
    • 我已经添加了cookie认证服务。AddAuthentication(options => { options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; }
    • 我已经更新了问题并添加了我所做的代码
    猜你喜欢
    • 2018-12-29
    • 2019-04-15
    • 2019-05-18
    • 2020-06-19
    • 2019-02-10
    • 2020-01-07
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    相关资源
    最近更新 更多