我正在努力为管理员提供使用ASP.NET身份向我的网站添加新用户的选项。
我的想法是,我的布局中有一个链接,将我重定向到帐户/注册视图
if (User.IsInRole(RoleName.IsAdmin))
{ //this is just for the admin
<ul class="nav navbar-nav">
//other links
<li>@Html.ActionLink("Add assistant", "Register", "Account")</li>
</ul>
}
这是我的帐户/注册操作,我将用户添加到角色&#34; IsAssistant&#34;。
[HttpPost]
[Authorize(Roles = RoleName.IsAdmin)]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.FirstName, Email = model.Email,kclusername=model.kclusername,
FirstName=model.FirstName,LastName=model.LastName};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var role = new RoleStore<IdentityRole>();
var roleManager = new RoleManager<IdentityRole>(role);
await roleManager.CreateAsync(new IdentityRole(RoleName.IsAssistant));
await UserManager.AddToRoleAsync(user.Id, RoleName.IsAssistant);
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
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);
}
// If we got this far, something failed, redisplay form
return View(model);
}
当我创建新用户时,我被重定向并以新用户身份登录。但是当我尝试再次登录时,我不能,并且我收到一条消息&#34;无效的登录尝试&#34;。 我没有修改登录操作:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
有什么问题?我错过了什么吗?感谢。