【问题标题】:In Identity Server 4, how to add actual users, not test users在 Identity Server 4 中,如何添加实际用户,而不是测试用户
【发布时间】:2021-03-18 15:13:53
【问题描述】:

我正在学习 Identity Server 4,让我感到沮丧的是,给出的所有示例都显示了如何通过中间件配置“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 的主要原因之一!

【问题讨论】:

    标签: c# .net .net-core identityserver4


    【解决方案1】:

    显然,如果你想使用真实的信息,你需要连接到你的数据库。

    连接数据库

    当局提供了非常详细的连接方案。这是连接解决方​​案。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
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2019-11-19
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多