ASP.NET 用户名更改

发布于 2024-07-30 14:44:23 字数 303 浏览 2 评论 0原文

我有一个使用 ASP.net 会员资格提供程序的 ASP.NET 站点。 数据库中的每个评论、条目等均由用户 ID 跟踪。

由于MS没有提供更改用户名的方法,因此我在数据库的“users”表中找到了userNAME,并且只有1个地方出现了用户名。

我的问题是,

提供允许用户编辑自己的用户名的“编辑个人资料”页面是否安全。 当然,我会通过直接更改数据库中的“用户名”值来在后台处理此更改。

这有什么缺点吗? 我创建并修改了一些测试帐户,似乎没问题,我只是想知道在将其投入生产之前是否有任何已知的负面影响。

I have an asp.net site which uses the ASP.net Membership provider. Each comment, entry etc in the DB is tracked by the userID.

Since MS doesn't provide a way to change the username, I've found the userNAME in the "users" table in the DB and there is only 1 place where the username appears.

My question is,

Is it safe to provide an "edit profile" page where the user is allowed to edit their own username. Of course i would handle this change in the background by directly changing the "username" value in the DB.

Are there any downsides to this ? I've created and modified some test accounts and it seems to be fine, i am just wondering if there is any known negatives to this before putting it into production.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2024-08-06 14:44:23

cptScarlet 的链接很好,但是如果不需要的话,我鄙视使用存储过程,并且只要有可能我就喜欢实体框架。 以下是我使用 EF 4.0 和 .NET 4.0 更改用户名所做的操作:

  1. 右键单击项目 -> 添加新项目 -> ADO.NET 实体数据模型
  2. 为其指定一个正确的名称,我选择“MembershipModel.edmx”,然后单击“添加”
  3. 选择“从数据库生成”,然后单击“下一步”
  4. 将连接添加到“aspnetdb”数据库(ASP.NET 成员数据库)
  5. 为其指定一个正确的名称名称,我选择“MembershipEntities”
  6. 单击下一步
  7. 钻取表并选择 aspnet_Users
  8. 将模型命名空间更改为 MembershipModel
  9. 单击完成

现在您可以添加代码来创建 EF 对象上下文并修改数据库:

public void ChangeUserName(string currentUserName, string newUserName)
{
    using (var context = new MembershipEntities())
    {
        // Get the membership record from the database
        var currentUserNameLowered = currentUserName.ToLower();
        var membershipUser = context.aspnet_Users
            .Where(u => u.LoweredUserName == currentUserNameLowered)
            .FirstOrDefault();

        if (membershipUser != null)
        {
            // Ensure that the new user name is not already being used
            string newUserNameLowered = newUserName.ToLower();
            if (!context.aspnet_Users.Any(u => u.LoweredUserName == newUserNameLowered))
            {
                membershipUser.UserName = newUserName;
                membershipUser.LoweredUserName = newUserNameLowered;
                context.SaveChanges();
            }
        }
    }
}

注意:我没有在我的应用程序 ID 中考虑应用程序 ID。代码。 我通常只有一个应用程序使用 ASP.NET 成员资格数据库,因此如果您有多个应用程序,则需要考虑到这一点。

cptScarlet's link was good, however I despise using stored procedures if I don't have to and I favor Entity Framework whenever possible. Here's what I did to change the user name, using EF 4.0 and .NET 4.0:

  1. Right click project -> Add New Item -> ADO.NET Entity Data Model
  2. Give it a proper name, I chose "MembershipModel.edmx" and click Add
  3. Select Generate from database and click Next
  4. Add the connection to your 'aspnetdb' database (the ASP.NET membership database)
  5. Give it a proper name, I chose "MembershipEntities"
  6. Click Next
  7. Drill into Tables and select aspnet_Users
  8. Change the Model Namespace to MembershipModel
  9. Click Finish

Now you can add code to create the EF object context and modify the database:

public void ChangeUserName(string currentUserName, string newUserName)
{
    using (var context = new MembershipEntities())
    {
        // Get the membership record from the database
        var currentUserNameLowered = currentUserName.ToLower();
        var membershipUser = context.aspnet_Users
            .Where(u => u.LoweredUserName == currentUserNameLowered)
            .FirstOrDefault();

        if (membershipUser != null)
        {
            // Ensure that the new user name is not already being used
            string newUserNameLowered = newUserName.ToLower();
            if (!context.aspnet_Users.Any(u => u.LoweredUserName == newUserNameLowered))
            {
                membershipUser.UserName = newUserName;
                membershipUser.LoweredUserName = newUserNameLowered;
                context.SaveChanges();
            }
        }
    }
}

Note: I did not account for application ID's in my code. I typically only ever have one application using the ASP.NET membership database, so if you have multiple apps, you'll need to account for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文