c# - 在 api 中访问 userManager
问题描述
我在 ASP.Net 核心中有一个应用程序,分为 3 个项目(API、数据、服务)。API 包含将调用服务项目中的方法的控制器(我将在其中处理数据)。
现在我正在使用 ASP 提供的身份验证系统,所以我在启动方法中添加了这些行。
services.AddIdentity<IdentityUser,IdentityRole>().AddEntityFrameworkStores<RHPDbContext>();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = "RHP User",
ValidIssuer = "MYIssuer",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("MyRHPSuperKey"))
};
});
由于我正在制作一个 API,我将需要基于令牌的身份验证
并且还在启动的configure方法中添加了这一行
app.UseAuthentication();
我现在的问题是如何访问userManager
我的服务项目中的,以便我可以添加用户,或获取用户,将角色属性分配给用户......等等
解决方案
这是您可以在控制器类中访问 userManager 的方式。
public AccountController(UserManager<IdentityUser> userManager)
推荐阅读
- firebase - Firebase 安全规则 - 未定义的资源错误
- r - 在 R 中使用 for 循环遍历数据帧的名称
- spring - 导致 UnsupportedMediaType 415 OpenFeign 的内容类型标头值
- python - 从按多列分组的 pandas 数据框中获取嵌套的 JSON
- api - Graph API 不会恢复邮件消息,而是创建一条新消息,其中 CreateDateTime 自动更新为当前日期
- model - 对这个(简单的)Optaplanner 用例进行建模的更好方法是什么?
- php - Laravel 查询生成器获取用户登录记录
- python - 如何在数据集中添加填充以填充列表中最多 50 个项目并将 NaN 替换为 0?
- qt - 在Qt中再次单击按钮时如何扭转这种情况?
- python - 文件不为空但是python以列表形式将文件输出为空?