显然,如果你想使用真实的信息,你需要连接到你的数据库。
连接数据库
当局提供了非常详细的连接方案。这是连接解决方案。http://docs.identityserver.io/en/latest/quickstarts/5_entityframework.html
自定义用户
一般来说,我们更喜欢自定义用户属性。您需要创建一个继承自 IdentityUser 的自定义用户。
public class ApplicationUser : IdentityUser<int>
{
// every attr you want
// ...
}
对于用户,需要自定义数据库上下文:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, ...>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){}
}
在Startup.cs 中,添加数据库上下文。
services.AddDbContext<ApplicationDbContext>(options => options.UseYourDbSystem(connectionString));
其实已经成功了。出于测试目的,您可以添加 UI。
添加用户界面
添加 UI 是为了更好地进行测试。完整的代码在官方 UI 中提供,您可以通过一些调整来运行它。您可以在您的项目中运行dotnet new is4ui 或创建一个新项目。这是 UI 解决方案。 http://docs.identityserver.io/en/latest/quickstarts/2_interactive_aspnetcore.html
修改登录页面内容
在Quickstart/Account/AccountController.cs,可以看到TestUser被注入到了构造函数中。删除它并注入以下内容:
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager
找到Login post action,修改登录验证逻辑。
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
}