是否可以使用Asp.Net Identity核心注册多个用户? [英] Is it possible to register more than one user with Asp.Net Identity core?

查看:123
本文介绍了是否可以使用Asp.Net Identity核心注册多个用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过使用实体框架创建用户服务并创建密码哈希,可以将一个或多个用户添加到该网站! 但是我想使用asp.net身份功能,即使我想注册批处理用户(例如使用excel或xml文件上传用户列表). 在我的情况下,我想通过在xml,json或excel文件中上传用户列表来在网站上注册多个用户. 我想在一次交易中将它们全部注册. 有人知道吗?

I know by creating an user service with entity framework and creating a password hash i can add one or more users to the website! but i'd like to use asp.net identity features even when i want to register batch users (like upload users list with excel or xml file). In my scenario i want register more than one users on the website by uploading users list in xml,json or excel file. And i want to register all of them in one transaction. Has anyone idea?

推荐答案

取决于您要使用的抽象级别,但是只需循环遍历默认Register方法的内容,您可以执行以下操作:

Depends at what level of abstraction you want to work at, but by just looping over the content of the default Register method, you could do something like this:

[HttpPost]
public async Task<IActionResult> RegisterLotsOfPeople(RegisterLotsModel model)
{
    var successful = new List<string>();
    var failed = new List<string>();
    foreach (var toRegister in model.ApplicationUsers)
    {
        var user = new ApplicationUser {UserName = toRegister.UserName, Email = toRegister.Email};
        var result = await _userManager.CreateAsync(user, toRegister.Password);
        if (result.Succeeded)
        {
            successful.Add(toRegister.UserName);
        }
        else
        {
            failed.Add(toRegister.UserName);
        }
    }

    return Json(new {SuccessfullyRegistered = successful, FailedToRegister = failed});
}

您必须将数据以JSON格式发布到终点.

You'd have to post the data to the end point in JSON format.

DTO类是:

public class RegisterLotsModel
{
    public List<UserToRegister> ApplicationUsers { get; set; }
}

public class UserToRegister
{
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

还有很多其他方法可以完成您要达到的目标,但这是快速,肮脏的,并且可能会很好地工作.如果需要电子邮件确认,则可能需要在方法的成功部分中发送确认电子邮件.

There's lots of other way to do what you're trying to achieve, but this is quick, dirty and would likely work fine. You may need to send the confirmation email in the success part of the method, if email confirmation is required.

要将所有新用户插入一个事务中,您必须首先生成其密码哈希,然后再将其插入数据库.

To insert all the new users in one transaction, you would have to generate their password hashes first, and then insert them into the database.

要获取给定密码的哈希,可以使用UserManager提供的密码哈希:

To get a hash of a given password, you can use the password hasher provided by the UserManager:

foreach (var toRegister in model.ApplicationUsers)
{
    var hasher = _userManager.PasswordHasher;
    var user = new ApplicationUser {UserName = toRegister.UserName, Email = toRegister.Email};
    toRegister.Hashed = hasher.HashPassword(user, toRegister.Password);
}

然后可以将它们手动插入数据库中.这对于基本的密码身份验证就足够了,但是如果您的实现使用诸如SecurityStamp之类的东西,则在创建用户时可能需要实现适当的方法.

You could then manually insert these into the database. This would be enough for basic password authentication, but if your implementation uses things like SecurityStamp, you would likely need to implement the appropriate methods when creating the user.

这篇关于是否可以使用Asp.Net Identity核心注册多个用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆