簡體   English   中英

從外部訪問用戶身份和聲明

[英]Access User.Identity and Claims from Outside

我正在嘗試使用OpenID Connect構建SSO(.net核心)服務,該服務將是Webforms應用程序和服務提供商之間的一層。 我創建了一些端點來檢查用戶是否已通過身份驗證,並從服務中獲取用戶聲明。 從瀏覽器調用這些端點時,我可以獲得正確的結果。 但是,當我從網站(使用HttpWebRequest)調用它們時。 User.Identity始終為空。 我的端點檢查用戶是否已通過身份驗證,如下所示:

[HttpGet]
[Route("IsAuthenticated")]
public IActionResult IsAuthenticated()
{
    return Json(User.Identity.IsAuthenticated);
}

或獲取訪問令牌:

[HttpGet]
[Route("access_token")]
public async Task<IActionResult> AccessToken()
{
    var tokenResult = await HttpContext.GetTokenAsync("access_token");
    return Json(tokenResult);
}

我的啟動ConfigureServices看起來像這樣:

var OIDCConfiguration = new OIDCSettings()
{
    Authority = Configuration["OIDCSettings:Authority"],
    ClientId = Configuration["OIDCSettings:ClientId"],
    ClientSecret = Configuration["OIDCSettings:ClientSecret"],
    CallbackPath = Configuration["OIDCSettings:CallbackPath"],
    UserInfoEndpoint = Configuration["OIDCSettings:UserInfoEndpoint"]
};

services.AddAuthentication(options => {
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect("oidc", options => {
    options.Authority = OIDCConfiguration.Authority;
    options.ClientId = OIDCConfiguration.ClientId;
    options.ClientSecret = OIDCConfiguration.ClientSecret;
    options.CallbackPath = new PathString(OIDCConfiguration.CallbackPath);
    options.ResponseType = OpenIdConnectResponseType.Code;
    options.GetClaimsFromUserInfoEndpoint = true;
    options.Scope.Add("openid emailAddress");
    options.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
    options.SaveTokens = true;
});

要啟動中間件,我正在使用ChallengeResult對象。

我是OpenID Connect和中間件體系結構的初學者,因此即使我的體系結構是正確的,我也不確定自己缺少什么。

因此,通常當您通過瀏覽器登錄時,ASP.NET Core會管理一個cookie,其中包含有關ClaimsPrincipal的所有相關信息,以便服務器可以在對網站的后續請求中識別出您。 可能發生的情況是您的服務器未使用身份驗證后管理的cookie發送請求,因此無法對您的請求進行身份驗證。

暫無
暫無

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

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