dav*_*ond 1 c# asp.net-mvc-4 simplemembership
我在MVC4应用程序中使用SimpleMembership,并且需要用户能够更改自己的用户名.
我有功能,所以当用户更改其用户名时,它可以工作.但是,当调用Roles.IsUserInrole()之类的东西时,它会失败,因为User.Identity.Name设置为他们登录的内容,而不是新值.数据库中不再存在该值,因为它们已更改其名称.
我看不到用户名更新登录用户上下文的方法.在大多数情况下,我可以在会话中存储用户ID并在进行查询时检索它,但我使用Roles方法在视图中显示失败的数据.
有任何想法吗?
谢谢!
这是我目前的(工作)解决方案:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(AccountEditModel model)
{
if (ModelState.IsValid)
{
if (HasSensitiveInformationChanged(model)) // model.EmailAddress.ToLower() != User.Identity.Name.ToLower()
{
if (Membership.ValidateUser(User.Identity.Name, model.Password)) // && WebSecurity.IsCurrentUser(User.Identity.Name)) //redundant?
{
using (UsersContext db = new UsersContext())
{
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.EmailAddress.ToLower() == User.Identity.Name.ToLower());
if (user != null)
{
user.EmailAddress = model.EmailAddress;
db.SaveChanges();
WebSecurity.Logout();
WebSecurity.Login(model.EmailAddress, model.Password);
return RedirectToAction("Index", "Search");
}
else
{
ModelState.AddModelError("", "Could not find user. Please try logging in again.");
}
}
}
else
{
ModelState.AddModelError("","Could not change email address. Please verify your password and try again.");
}
}
else
{
//no change
return RedirectToAction("Index", "Search");
}
}
return View("Index", model);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1903 次 |
| 最近记录: |