ASP.NET 2.0의 sql 멤버 자격 공급자는 사용자 이름을 변경할 수 없습니다. 사용자 이름을 변경할 수는 있지만 사용자 정의 구현을 사용해야합니다.
동일한 사용자 이름이지만 새로운 UserId로 사용자를 다시 만들지 않으려면 회원 쿠키를 새 사용자 이름으로 업데이트해야합니다.
아래 예제에서는 Linq를 사용하여 SQL에서 멤버십 테이블을 업데이트합니다. MembershipDataContext라는 데이터 컨텍스트가 있습니다.
public bool ChangeUserName(Guid userId, string newUserName)
{
bool success = false;
newUserName = newUserName.Trim();
// Make sure there is no user with the new username
if (Membership.GetUser(newUserName) == null)
{
MembershipUser u = Membership.GetUser(userId);
string oldUsername = u.UserName;
// get current application
MembershipDataContext context = new MembershipDataContext();
aspnet_User userToChange = (from user in context.aspnet_Users
where user.UserId == userId
select user).FirstOrDefault();
if (userToChange != null)
{
userToChange.UserName = newUserName;
userToChange.LoweredUserName = newUserName.ToLower();
context.SubmitChanges();
// ASP.NET Issues a cookie with the user name.
// When a request is made with the specified cookie,
// ASP.NET creates a row in aspnet_users table.
// To prevent this sign out the user and then sign it in
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie =
HttpContext.Current.Request.Cookies[cookieName];
FormsAuthenticationTicket authTicket = null;
try
{
authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
FormsIdentity formsIdentity =
new FormsIdentity(
new FormsAuthenticationTicket(
authTicket.Version,
newUserName,
authTicket.IssueDate,
authTicket.Expiration,
authTicket.IsPersistent,
authTicket.UserData));
string y = HttpContext.Current.User.Identity.Name;
string[] roles =
authTicket.UserData.Split(new char[] { '|' });
System.Security.Principal.GenericPrincipal genericPrincipal =
new System.Security.Principal.GenericPrincipal(
formsIdentity,
roles);
HttpContext.Current.User = genericPrincipal;
}
catch (ArgumentException ex)
{
// Handle exceptions
}
catch(NullReferenceException ex)
{
// Handle exceptions
}
FormsAuthentication.SignOut();
HttpContext.Current.Session.Abandon();
FormsAuthentication.SetAuthCookie(newUserName, false);
success = true;
}
}
return success;
}
나는 문제가 문제가 사용자가 이전 멤버 이름에 대한 기존 멤버십 개체에 역할을 할당하는 것과 관련이 있다는 것을 알게되었습니다. 새 역할 행이 자동으로 추가되고 역할 클래스가 작성한 사용자 ID가 추가됩니다. – zsharp
그럼 문제를 해결하기 위해 무엇을 했습니까? – CompanyDroneFromSector7G