从外部访问 User.Identity 和声明
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);
}
我的启动配置服务看起来像这样:
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 的所有相关信息,以便服务器可以在后续对网站的请求中识别您。可能发生的情况是您的服务器没有发送带有已管理 post 身份验证的 cookie 的请求,因此无法对您的请求进行身份验证。
我正在尝试使用 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);
}
我的启动配置服务看起来像这样:
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 的所有相关信息,以便服务器可以在后续对网站的请求中识别您。可能发生的情况是您的服务器没有发送带有已管理 post 身份验证的 cookie 的请求,因此无法对您的请求进行身份验证。