簡體   English   中英

如何驗證用戶的 cookie 並在身份框架中提取他們的聲明?

[英]How do I validate a user's cookie and extract their claims in identity framework?

我目前正在使用 Identity 框架為用戶創建和存儲 cookie。 當用戶嘗試使用 cookie 登錄時,我無法從 cookie 中獲取用戶聲明。 有沒有辦法在傳入cookie時對其進行解密或在httpcontext中找到它?

我已經嘗試搜索 httpcontext,並且我目前正在嘗試找到一種方法來解密傳入的 cookie。

從startup.cs

 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
                {
                    options.Cookie.Name = "MyCookie.Identity";

                    options.Cookie.Expiration = TimeSpan.FromDays(1);
                });

我在哪里創建 cookie:


        private async void AddUserCookie(AuthRequest authRequest)
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, authRequest.UserName),
                new Claim(ClaimTypes.Name, authRequest.UserName),
                new Claim(ClaimTypes.Email, "TestClaim@Test.com")
            };

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                AllowRefresh = true,
                ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1),
                IsPersistent = true,
                IssuedUtc = DateTimeOffset.UtcNow
            };

            await this._httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties).ConfigureAwait(false);

當我嘗試從 http 上下文中檢索 cookie 時,它​​說用戶身份中沒有聲明。

答案在此處提供: https : //forums.asp.net/t/2157350.aspx?How+does+cookie+authentication+in+identity+framework+work+

簡而言之,我忘了在我的 startup.cs 中添加 app.UseAuthentication()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM