La modifica del nome utente non è supportata dal provider di appartenenza SQL in ASP.NET 2.0. Puoi ancora cambiare il nome utente, ma devi usare l'implementazione personalizzata.
Inoltre, è necessario aggiornare il cookie di appartenenza con il nuovo nome utente per evitare di ricreare l'utente con lo stesso nome utente ma nuovo ID utente.
Nell'esempio seguente, utilizzo Linq su SQL per aggiornare le tabelle di appartenenza: il contesto dati è denominato 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;
}
fonte
2010-01-28 09:08:39
ho capito che il problema ha a che fare con l'assegnazione di un ruolo al vecchio oggetto di appartenenza per il vecchio nome degli utenti che non esisteva. quindi è stata aggiunta automaticamente una nuova riga di ruoli e un ID utente creato dalle classi dei ruoli. – zsharp
Quindi, cosa hai fatto per risolvere il problema? – CompanyDroneFromSector7G