[英]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.