简体   繁体   English

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

[英]ASP.NET Assigning default user roles upon registration

How can I assign default roles eg standard user upon user registration?如何在用户注册时分配默认角色,例如标准用户?

At the moment, I can manually assign roles by editing the table in SQL server but is there a way that a default role can assigned to all users?目前,我可以通过在 SQL Server 中编辑表来手动分配角色,但是有没有办法将默认角色分配给所有用户?

I've tried using the solution posted here but it doesn't appear to work for projects using the ASP.NET Core Web Application (Visual Studio 2015).我已尝试使用此处发布的解决方案但它似乎不适用于使用 ASP.NET Core Web 应用程序 (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);
    }

I assume you use the default Asp .Net membership and role providers.我假设您使用默认的 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);
 }

above code helps you to make it working.上面的代码可以帮助您使其工作。

Stumbled across this.偶然发现了这一点。 I had the same problem.我有同样的问题。 This worked for me.这对我有用。

        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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Asp.net Core中注册时添加用户角色 - How to add user roles upon registrations in Asp.net Core asp.net从mysql向用户/会话分配角色 - asp.net assigning roles to a user/Session from mysql 在 ASP.NET 核心 Web Z72664DC0959F3B0C0470479 中注册后,将 email 确认令牌作为可点击链接发送给用户 - Send email confirmation token as clickable link to user upon registration in ASP.NET Core Web Api 在C#ASP.NET Web表单中为用户分配角色 - Assigning Roles to Users in C# ASP.NET Web Forms 在ASP.NET MVC中为角色分配访问权限 - Assigning access rights to roles in asp.net mvc 用户和角色成表asp.net MVC - User & roles into a table asp.net mvc ASP.Net MVC用户列表及其角色 - ASP.Net MVC List of User and their Roles 使用默认的(内置)asp.net用户注册并使用我自己的数据库登录已部署的azure网站 - Use default (built-in) asp.net user registration and login in deployed azure website with my own database 种子 ASP.NET .NET Core 3.1 中的身份用户和角色 - Seed ASP.NET Identity user and roles in .NET Core 3.1 ASP.Net 5 + Identity 3覆盖默认角色实现 - ASP.Net 5 + Identity 3 Overriding default Roles implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM