我們試圖了解身份驗證 cookie(ASP.NET Core 5.0 - Microsoft.AspNetCore.Authentication.OpenIdConnect5.0.11 版)如何與沒有 PKCE 的授權代碼流一起作業。
認證程序
身份驗證程序如下所示:前端中的登錄重定向到 的登錄端點AuthController并啟動 OpenId Connect 程序。因此,您已通過身份提供者的身份驗證,并為用戶設定了 cookie。每次呼叫 API 時都會發送它們以檢查請求是否經過身份驗證。
在此程序中創建了 3 個 cookie:
餅干#1:
- 名稱 = .AspNetCore.Cookies
- 值 = chunks-2
餅干#2:
- 名稱 = .AspNetCore.CookiesC1
- 值 = CfDJ8GRK-GHfascFTvp0o_E7oKZU-6GOAbUGCPHZZPfewEv12PmKgr46gfeTQC351e-Jnxq8SxzjJEgboIedIPCO11Q […]
餅干 #3:
- 名稱 = .AspNetCore.CookiesC2
- 值 = 8G86qN27NOS2Z-75XqY34d-ID1nOELpPaHUIe2EkFZMmfjrYSKA2JaU30p4Ozh8RyxZXTpFCRV8
問題
- 這些
.AspNetCorecookie如何用于身份驗證? - 如何生成名稱和加密值?
- 這些 cookie 包含什么?
我們嘗試解密 cookie(如何手動解密 ASP.NET Core 身份驗證 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;
}
uj5u.com熱心網友回復:
.AspNetCore cookie 由 Cookie 身份驗證處理程式在用戶成功通過 OpenIDConnect 處理程式進行身份驗證(被質詢)后創建。
如果 cookie 大小太大,那么它將被分成 4Kb 的塊,以確保 cookie 不會被瀏覽器或代理拒絕。
cookie 中的資料使用資料保護 API 進行加密,您可以通過一些努力使用資料保護 API解密 cookie 的內容。
cookie 中的資料主要包含您的 ClaimsPrincipal(用戶物件)及其各種宣告。(可選)您還可以將 openid-connect 令牌存盤在 cookie 中。
希望這能回答您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/381736.html
