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

时间:2016-11-06 04:50:13

标签: c# asp.net asp.net-mvc

如何指定默认角色,例如用户注册后的标准用户?

目前,我可以通过编辑SQL Server中的表来手动分配角色,但有没有一种方法可以为所有用户分配默认角色?

我尝试使用已发布的解决方案here,但它似乎不适用于使用ASP.NET核心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);
    }

2 个答案:

答案 0 :(得分:0)

我假设您使用默认的Asp .Net成员资格和角色提供程序。

Functions funcs = new Functions();
funcs.errEmailV2(someVariableThatisAString, someIntVariable);

public void errEmailV2(params object[] paths)
{
    string strVars= String.Join(", ", paths);
    sendEmail("me@email","emailSubject",strVars);  
}

上面的代码可以帮助您实现目标。

答案 1 :(得分:0)

偶然发现。我有同样的问题。这对我有用。

        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);
        }
相关问题