ASP.在注册时分配默认用户角色

本文关键字:默认 用户 角色 分配 注册 ASP | 更新日期: 2023-09-27 18:16:43

如何在用户注册时分配默认角色,例如标准用户?

目前,我可以通过在SQL服务器中编辑表来手动分配角色,但是是否有一种方法可以将默认角色分配给所有用户?

我已经尝试使用解决方案张贴在这里,但它似乎不工作的项目使用ASP。. NET Core Web Application (Visual Studio 2015).

// POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                // Send an email with this link
                //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "User created a new account with password.");
                return RedirectToLocal(returnUrl);
            }
            AddErrors(result);
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

ASP.在注册时分配默认用户角色

我假设您使用默认的asp.net成员和角色提供程序。

if (result.Succeeded)
{
Roles.AddUserToRole(user.UserName, "<role>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
 return RedirectToLocal(returnUrl);
 }
上面的

代码可以帮助你使它工作

偶然发现了这个。我也有同样的问题。

        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                string rolname = "User";                    
                var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
                await userManager.AddToRoleAsync(user.Id, rolname);
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href='"" + callbackUrl + "'">here</a>");
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }