使用开放 ID 连接服务器检索 asp.net 核心中的声明
Retrieving claims in asp.net core using open id connect server
我即将在我的 asp.net 核心应用程序中实施基于承载的身份验证。来自 .NET Framework,核心内容对我来说仍然很新。从服务器获取令牌已经很有效了。但是我如何在以下请求中确定用户是否已通过身份验证?在 .NET Framework 项目中,我曾经使用
(ClaimsIdentity)Thread.CurrentPrincipal.Identity.IsAuthenticated;
但是,此 returns 身份声明为空或默认。这是我目前的设置:
我从 OpenIdConnect.Server framework and the sample code in their Get Started 部分开始。这很好用,我的客户收到了 Bearer Token。我通过以下方式在我的 Startup.cs
中构建它:
public class Startup
{
[...]
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddAuthentication();
[...]
}
public void Configure([...])
{
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
app.UseOpenIdConnectServer(options =>
{
[code of example]
}
}
在客户端,我将检索到的令牌用于进一步的请求
现在,我现在如何访问当前登录的用户声明或我如何知道 he/she 是否已通过身份验证?
我试过了
// within api controller:
var isAuth = this.User.Identity.IsAuthenticated
// using DI
public class MyClass(IHttpContextAccessor httpContextAccessor) {
public void MyMethod() {
var isAuth = httpContextAccessor.HttpContext.User.Identity.IsAuthenticated;
}
}
但这总是 returns false
并且声明是一些默认值。
我错过了什么吗?我需要安装一些额外的服务或中间件吗?
OpenID Connect 服务器中间件需要注意的一件事是它不会为您验证传入的访问令牌(它只会发出它们)。由于您使用的是默认令牌格式(加密),因此您可以为此使用 AspNet.Security.OAuth.Validation
包:
public class Startup
{
[...]
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddAuthentication();
[...]
}
public void Configure([...])
{
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseOpenIdConnectServer(options =>
{
[code of example]
});
app.UseOAuthValidation();
app.UseMvc();
}
}
我即将在我的 asp.net 核心应用程序中实施基于承载的身份验证。来自 .NET Framework,核心内容对我来说仍然很新。从服务器获取令牌已经很有效了。但是我如何在以下请求中确定用户是否已通过身份验证?在 .NET Framework 项目中,我曾经使用
(ClaimsIdentity)Thread.CurrentPrincipal.Identity.IsAuthenticated;
但是,此 returns 身份声明为空或默认。这是我目前的设置:
我从 OpenIdConnect.Server framework and the sample code in their Get Started 部分开始。这很好用,我的客户收到了 Bearer Token。我通过以下方式在我的 Startup.cs
中构建它:
public class Startup
{
[...]
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddAuthentication();
[...]
}
public void Configure([...])
{
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
app.UseOpenIdConnectServer(options =>
{
[code of example]
}
}
在客户端,我将检索到的令牌用于进一步的请求
现在,我现在如何访问当前登录的用户声明或我如何知道 he/she 是否已通过身份验证?
我试过了
// within api controller:
var isAuth = this.User.Identity.IsAuthenticated
// using DI
public class MyClass(IHttpContextAccessor httpContextAccessor) {
public void MyMethod() {
var isAuth = httpContextAccessor.HttpContext.User.Identity.IsAuthenticated;
}
}
但这总是 returns false
并且声明是一些默认值。
我错过了什么吗?我需要安装一些额外的服务或中间件吗?
OpenID Connect 服务器中间件需要注意的一件事是它不会为您验证传入的访问令牌(它只会发出它们)。由于您使用的是默认令牌格式(加密),因此您可以为此使用 AspNet.Security.OAuth.Validation
包:
public class Startup
{
[...]
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddAuthentication();
[...]
}
public void Configure([...])
{
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseOpenIdConnectServer(options =>
{
[code of example]
});
app.UseOAuthValidation();
app.UseMvc();
}
}