在Asp.Net成员手动更改用户名 [英] Manually changing username in Asp.Net Membership

查看:92
本文介绍了在Asp.Net成员手动更改用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过直接访问asp.net会员用户表更改用户名。但是,老的用户名是一个新行pserved和asp.net自动分配一个新的用户名$ P $。我如何阻止这种情况的发生?

编辑:只在用户表和角色表,而不是会员表

  VAR MUSER = dc.aspnet_Users
            。凡(U => u.UserId ==(GUID)user.ProviderUserKey)
            。选择(U => U).SingleOrDefault();mUser.UserName =了newName;
mUser.LoweredUserName = newName.ToLower();尝试
{
    dc.SubmitChanges();
}
抓住
{
    ...
}


解决方案

更改用户名不是由ASP.NET 2.0中的SQL成员资格提供支持。您仍可以更改用户名,但你必须使用自定义实现。

你也需要更新为新的用户名会员cookie,以避免使用相同的用户名,但新用户ID的用户的娱乐。

在下面我使用LINQ到SQL来更新成员表的例子 - 我有一个名为MembershipDataContext数据上下文。

 公共BOOL ChangeUserName(GUID userid,字符串newUserName)
{
    布尔成功= FALSE;
    newUserName = newUserName.Trim();    //确保有新的用户不
    如果(Membership.GetUser(newUserName)== NULL)
    {
        的MembershipUser U = Membership.GetUser(用户ID);
        字符串oldUsername = u.UserName;
        //获取当前的应用程序        MembershipDataContext语境=新MembershipDataContext();
        aspnet_User userToChange =(从用户context.aspnet_Users
                                    其中,user.UserId ==用户id
                                    选择用户).FirstOrDefault();        如果(userToChange!= NULL)
        {
            userToChange.UserName = newUserName;
            userToChange.LoweredUserName = newUserName.ToLower();            context.SubmitChanges();            // ASP.NET问题与用户名的cookie。
            //当请求与指定的cookie发,
            // ASP.NET创建于aspnet_Users表格内的一行。
            //为prevent此注销用户,然后签字在            字符串cookieName = FormsAuthentication.FormsCookieName;
            的HttpCookie authCookie =
              HttpContext.Current.Request.Cookies [cookieName]            的FormsAuthenticationTicket authTicket = NULL;            尝试
            {
                authTicket =
                    FormsAuthentication.Decrypt(authCookie.Value);                FormsIdentity formsIdentity =
                    新FormsIdentity(
                        新的FormsAuthenticationTicket(
                            authTicket.Version,
                            newUserName,
                            authTicket.IssueDate,
                            authTicket.Expiration,
                            authTicket.IsPersistent,
                            authTicket.UserData));                字符串Y = HttpContext.Current.User.Identity.Name;
                字符串[] =角色
                    authTicket.UserData.Split(新的char [] {'|'});
                System.Security.Principal.GenericPrincipal的GenericPrincipal =
                    新System.Security.Principal.GenericPrincipal(
                                                        formsIdentity,
                                                        角色);                HttpContext.Current.User =的GenericPrincipal;
            }
            赶上(ArgumentException的前)
            {
                //处理异常
            }
            赶上(前的NullReferenceException)
            {
                //处理异常
            }            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Abandon();
            FormsAuthentication.SetAuthCookie(newUserName,FALSE);
            成功= TRUE;
        }
    }    返回成功;
}

I am able to change a username by directly accessing the asp.net membership user tables. However, the old username is preserved in a new row and assigned a new UserID automatically by asp.net. How do I stop that from happening?

EDIT: only in the users table and roles table, not the membership table.

var mUser = dc.aspnet_Users
            .Where(u => u.UserId == (Guid)user.ProviderUserKey)
            .Select(u => u).SingleOrDefault();

mUser.UserName = newName;
mUser.LoweredUserName = newName.ToLower();

try
{
    dc.SubmitChanges();
}
catch
{
    ...
}

解决方案

Changing username is not supported by the sql membership provider in ASP.NET 2.0. You can still change the username but you have to use custom implementation.

Also you have to update the membership cookie with the new username in order to avoid recreation of the user with the same username but new UserId.

In the example below I use Linq to SQL to update the membership tables - I have data context called 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;
}

这篇关于在Asp.Net成员手动更改用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆