如何在 net core 3 和身份服务器上使用 create react app -auth 模板接收当前用户
How to receive the current user with create react app -auth template on net core 3 and identity server
我以前从未使用过身份服务器,我想知道如果他们已经使用 net core 3 进行了身份验证,如何接收当前用户信息。
使用准系统创建 react app -auth weather app 模板
在 WeatherForecastController
中我们有授权属性:
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
具有以下端点:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
如果您已登录并被授予 jwt 不记名令牌,这将 return 正确的数据。
我想创建一个端点:
1. 允许匿名访问(登录或未登录,因此需要放弃或覆盖授权属性)
2. 然后它应该尝试验证用户
3.If 可以识别用户,然后端点将 return 正确的数据,否则如果无法识别用户,它将 return 数据的子集
我正在努力检索发出请求信息的用户,我相信我应该使用 GET /connect/userinfo
identityserver 端点并将承载令牌作为参数,但我不知道该怎么做这个。我想访问 user_id 以便我可以与 identityUser table.
对应
我也试过:
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
感觉好像有一种正确的方法来做我所缺少的东西。如果有人能为我指明正确的方向,我将不胜感激。
- It should then try to authenticate the user
浏览器与网络应用程序进行通信。您应该有一个 Web 应用程序能够重定向回 IDS4 以登录。你所拥有的是 API。后来的 Web 应用程序与 web APIs 进行通信。阅读更多 here
- If the user can be identified then the endpoint will return the correct data else if the user cannot be identified it will return a subset of the data
您正在寻找的是授权,您可以在端点上手动执行此检查。您可以使用 ControllerBase.User
访问当前用户,因为将有效的 JWT
令牌传递给 API。考虑到您已经使用如下代码设置身份验证:
services.AddAuthentication("Bearer").AddJwtBearer("Bearer",
options =>
{
options.Authority = "http://localhost:5000";
options.Audience = "api1";
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"//To set Identity.Name
};
});
I'm struggling to retrieve the user who has made the request information
您可以简单地使用 ControllerBase.User
获取 API 上的当前经过身份验证的用户,无需调用任何端点。
更新: 如上代码所示设置 NameClaimType
会将 Identity.Name
设置为用户标识符。
我以前从未使用过身份服务器,我想知道如果他们已经使用 net core 3 进行了身份验证,如何接收当前用户信息。
使用准系统创建 react app -auth weather app 模板
在 WeatherForecastController
中我们有授权属性:
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
具有以下端点:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
如果您已登录并被授予 jwt 不记名令牌,这将 return 正确的数据。
我想创建一个端点:
1. 允许匿名访问(登录或未登录,因此需要放弃或覆盖授权属性)
2. 然后它应该尝试验证用户
3.If 可以识别用户,然后端点将 return 正确的数据,否则如果无法识别用户,它将 return 数据的子集
我正在努力检索发出请求信息的用户,我相信我应该使用 GET /connect/userinfo
identityserver 端点并将承载令牌作为参数,但我不知道该怎么做这个。我想访问 user_id 以便我可以与 identityUser table.
我也试过:
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
感觉好像有一种正确的方法来做我所缺少的东西。如果有人能为我指明正确的方向,我将不胜感激。
- It should then try to authenticate the user
浏览器与网络应用程序进行通信。您应该有一个 Web 应用程序能够重定向回 IDS4 以登录。你所拥有的是 API。后来的 Web 应用程序与 web APIs 进行通信。阅读更多 here
- If the user can be identified then the endpoint will return the correct data else if the user cannot be identified it will return a subset of the data
您正在寻找的是授权,您可以在端点上手动执行此检查。您可以使用 ControllerBase.User
访问当前用户,因为将有效的 JWT
令牌传递给 API。考虑到您已经使用如下代码设置身份验证:
services.AddAuthentication("Bearer").AddJwtBearer("Bearer",
options =>
{
options.Authority = "http://localhost:5000";
options.Audience = "api1";
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"//To set Identity.Name
};
});
I'm struggling to retrieve the user who has made the request information
您可以简单地使用 ControllerBase.User
获取 API 上的当前经过身份验证的用户,无需调用任何端点。
更新: 如上代码所示设置 NameClaimType
会将 Identity.Name
设置为用户标识符。