在Identity Server 4中,如何添加实际用户而不是测试用户 [英] In Identity Server 4, how to add actual users, not test users
问题描述
我正在学习Identity Server 4,让我感到沮丧的是,给出的所有示例都显示了如何通过中间件配置'AddTestUsers()'添加Test(演示样本)用户.
I'm learning Identity Server 4 and it frustrates me that all examples given show how to add Test(Demo Sample) Users, through a middleware config 'AddTestUsers()'.
var builder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
// see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
options.EmitStaticAudienceClaim = true;
})
.AddTestUsers(TestUsers.Users)
一个人如何实际添加自己的用户?
我想在这里提出问题,以便将来其他新手可以解决这个问题.添加用户(用户名+密码)是使用Identity Server的主要原因之一!
I thought of asking the question here so that other newbies in the future can get their head around this. Adding Users (Username + Password) is one of the main reasons for using Identity Server!
推荐答案
很显然,如果您想使用真实信息,则需要连接到数据库.
Obviously, if you want to use real information, you need to connect to your database.
当局提供了非常详细的连接方案.这是连接解决方案. http://docs.identityserver.io/en/latest/quickstarts/5_entityframework.html
The authorities provide a very detailed connection scheme. Here is connection solution.http://docs.identityserver.io/en/latest/quickstarts/5_entityframework.html
通常,我们更喜欢自定义用户属性.您需要创建一个从 IdentityUser
继承的自定义用户.
In general, we prefer custom user attributes. You need to create a custom user that inherits from IdentityUser
.
public class ApplicationUser : IdentityUser<int>
{
// every attr you want
// ...
}
与用户一起,需要定制数据库上下文:
With the user, the DB context needs to be customized:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, ...>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){}
}
在 Startup.cs
中,添加数据库上下文.
In Startup.cs
, add DB context.
services.AddDbContext<ApplicationDbContext>(options => options.UseYourDbSystem(connectionString));
事实上,它已经成功了.为了进行测试,您可以添加用户界面.
In fact, it is already successful. For testing purposes, you can add the UI.
已添加UI,以进行更好的测试.完整的代码在官方UI中提供,您可以通过一些调整来运行它.您可以在项目中运行 dotnet new is4ui
或创建一个新项目.这是UI解决方案. http://docs.identityserver.io/en/latest/quickstarts/2_interactive_aspnetcore.html
The UI was added for better testing. The full code is provided in the official UI, and you can run it with a few tweaks. You can run dotnet new is4ui
in your project or create a new project. Here is UI solution. http://docs.identityserver.io/en/latest/quickstarts/2_interactive_aspnetcore.html
在 Quickstart/Account/AccountController.cs
中,您可以看到 TestUser
已注入到构造函数中.删除它并注入以下内容:
At Quickstart/Account/AccountController.cs
, you can see that the TestUser
is injected in the constructor. Delete it and inject the following:
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager
找到 Login
发布操作,修改登录验证逻辑.
Find Login
post action, Modify the login validation logic.
public async Task<IActionResult> Login(LoginInputModel model, string button)
{
// some other code
// ...
// This login logic
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Username);
if (user != null)
{
var result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberLogin, true);
if (result.Succeeded)
{
// Here you can do a few things after a successful login
// ...
await _events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.UserName));
}
if (_interaction.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl))
{
return Redirect(model.ReturnUrl);
}
return Redirect("~/");
}
}
// other code
}
这篇关于在Identity Server 4中,如何添加实际用户而不是测试用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!