在Asp.Net Membership中手动更改用户名

前端之家收集整理的这篇文章主要介绍了在Asp.Net Membership中手动更改用户名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以通过直接访问asp.net成员资格用户表来更改用户名.但是,旧用户名保留在新行中,并由asp.net自动分配新的UserID.我如何阻止这种情况发生?

编辑:仅在users表和角色表中,而不是在成员资格表中.

  1. var mUser = dc.aspnet_Users
  2. .Where(u => u.UserId == (Guid)user.ProviderUserKey)
  3. .Select(u => u).SingleOrDefault();
  4.  
  5. mUser.UserName = newName;
  6. mUser.LoweredUserName = newName.ToLower();
  7.  
  8. try
  9. {
  10. dc.SubmitChanges();
  11. }
  12. catch
  13. {
  14. ...
  15. }

解决方法

ASP.NET 2.0中的sql成员资格提供程序不支持更改用户名.您仍然可以更改用户名,但必须使用自定义实现.

此外,您必须使用新用户名更新成员资格cookie,以避免使用相同的用户名重新创建用户,但新的UserId.

在下面的示例中,我使用Linq to sql来更新成员资格表 – 我有名为MembershipDataContext的数据上下文.

  1. public bool ChangeUserName(Guid userId,string newUserName)
  2. {
  3. bool success = false;
  4. newUserName = newUserName.Trim();
  5.  
  6. // Make sure there is no user with the new username
  7. if (Membership.GetUser(newUserName) == null)
  8. {
  9. MembershipUser u = Membership.GetUser(userId);
  10. string oldUsername = u.UserName;
  11. // get current application
  12.  
  13. MembershipDataContext context = new MembershipDataContext ();
  14. aspnet_User usertochange = (from user in context.aspnet_Users
  15. where user.UserId == userId
  16. select user).FirstOrDefault();
  17.  
  18. if (usertochange != null)
  19. {
  20. usertochange.UserName = newUserName;
  21. usertochange.LoweredUserName = newUserName.ToLower();
  22.  
  23. context.SubmitChanges();
  24.  
  25. // ASP.NET Issues a cookie with the user name.
  26. // When a request is made with the specified cookie,// ASP.NET creates a row in aspnet_users table.
  27. // To prevent this sign out the user and then sign it in
  28.  
  29. string cookieName = FormsAuthentication.FormsCookieName;
  30. HttpCookie authCookie =
  31. HttpContext.Current.Request.Cookies[cookieName];
  32.  
  33. FormsAuthenticationTicket authTicket = null;
  34.  
  35. try
  36. {
  37. authTicket =
  38. FormsAuthentication.Decrypt(authCookie.Value);
  39.  
  40. FormsIdentity formsIdentity =
  41. new FormsIdentity(
  42. new FormsAuthenticationTicket(
  43. authTicket.Version,newUserName,authTicket.IssueDate,authTicket.Expiration,authTicket.IsPersistent,authTicket.UserData));
  44.  
  45. string y = HttpContext.Current.User.Identity.Name;
  46. string[] roles =
  47. authTicket.UserData.Split(new char[] { '|' });
  48. System.Security.Principal.GenericPrincipal genericPrincipal =
  49. new System.Security.Principal.GenericPrincipal(
  50. formsIdentity,roles);
  51.  
  52. HttpContext.Current.User = genericPrincipal;
  53. }
  54. catch (ArgumentException ex)
  55. {
  56. // Handle exceptions
  57. }
  58. catch( NullReferenceException ex)
  59. {
  60. // Handle exceptions
  61. }
  62.  
  63. FormsAuthentication.SignOut();
  64. HttpContext.Current.Session.Abandon();
  65. FormsAuthentication.SetAuthCookie(newUserName,false);
  66. success = true;
  67. }
  68. }
  69.  
  70. return success;
  71. }

猜你在找的asp.Net相关文章