ASP.NET Core 5 OpenIdConnect 身份验证 cookie 在理论上如何工作?

How do the ASP.NET Core 5 OpenIdConnect authentication cookies work in theory?

我们正在尝试了解身份验证 cookie(ASP.NET Core 5.0 - Microsoft.AspNetCore.Authentication.OpenIdConnect 版本 5.0.11)如何在没有 PKCE 的情况下使用授权代码流。

授权过程

auth 过程如下所示:前端的登录重定向到 AuthController 的登录端点并启动 OpenId Connect 过程。因此,您已通过身份提供者的身份验证,并且为用户设置了 cookie。随 API 的每次调用一起发送,以检查请求是否已通过身份验证。

在此过程中创建了 3 个 cookie:

Cookie #1:

Cookie #2:

Cookie #3:

问题

我们试图解密 cookie () 以了解其工作原理,但这对我们不起作用。

遗憾的是,我们还没有找到理论上如何生成 cookie(具有名称和值)的答案。

我希望这些问题是可以理解的,如果有人能回答我将不胜感激。

为了更好地理解代码片段。希望 :)

AuthController:

// https://auth0.com/blog/backend-for-frontend-pattern-with-auth0-and-dotnet/
public class AuthController : Controller
{
    public ActionResult Login(string returnUrl = "/login")
    {
        return new ChallengeResult(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties() { RedirectUri = returnUrl });
    }

    [Authorize]
    public async Task<ActionResult> Logout()
    {
        await HttpContext.SignOutAsync();

        return new SignOutResult(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties
        {
            //RedirectUri = Url.Action("Index", "Home")
            RedirectUri = "/logout"
        });
    }

    //[Authorize]
    public ActionResult GetUser()
    {
        var jsonReturn = new Dictionary<string, string>();

        if (User != null && User.Identity.IsAuthenticated)
        {
            jsonReturn.Add("isAuthenticated", "true");

            foreach (var claim in ((ClaimsIdentity)this.User.Identity).Claims)
            {
                jsonReturn.Add(claim.Type, claim.Value);
            }

            return Json(JsonConvert.SerializeObject(jsonReturn));
        }

        jsonReturn.Add("isAuthenticated", "false");
        return Json(JsonConvert.SerializeObject(jsonReturn));
    }
}

启动:

public void ConfigureServices(IServiceCollection services)
{
     JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

     services.AddAuthentication(options =>
     {
          options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
          options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
          options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
     })
     .AddCookie(o =>
     {
          o.Cookie.SecurePolicy = CookieSecurePolicy.Always;
          o.Cookie.SameSite = SameSiteMode.Strict;
          o.Cookie.HttpOnly = true;
     })
     .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options => ConfigureOpenIdConnect(options));
}

private void ConfigureOpenIdConnect(OpenIdConnectOptions options)
{
        options.Authority = <identity provider url>;
        options.ClientId = "<clientId>";
        options.ClientSecret = "<clientSecret>";

        options.ResponseMode = OpenIdConnectResponseMode.FormPost;
        options.Scope.Clear();
        options.Scope.Add("openid");
        options.Scope.Add("profile");
        options.Scope.Add("offline_access");
        
        options.CallbackPath = new PathString("/callback");
        options.SaveTokens = true;
        options.UseTokenLifetime = false;
}

.AspNetCore cookie 由 Cookie 身份验证处理程序在用户成功通过 OpenIDConnect 处理程序进行身份验证(被质询)后创建。

如果 cookie 太大,则会将其分成 4Kb 的块,以确保 cookie 不会被浏览器或代理拒绝。

cookie 中的数据使用 Data Protection API 加密,您可以通过一些努力使用数据保护 aPI.

解密 cookie 的内容

cookie 中的数据主要包含您的 ClaimsPrincipal(用户对象)及其各种声明。您还可以选择将 openid-connect 令牌存储在 cookie 中。

希望这能回答您的问题。