以电子邮件作为登录名的个人资料系统/会员系统

发布于 2024-07-25 20:40:42 字数 143 浏览 2 评论 0 原文

ASP.NET 会员系统使用唯一的用户名来识别用户。 如果我需要通过电子邮件登录用户怎么办?

我可以将电子邮件传递到用户名属性中,但事情会变得有点混乱。 用户可能想要更改电子邮件地址。

是否有任何资源可以描述实现它的好方法?

The ASP.NET Membershipsystem uses a unique username to identify a user. What if I need to login users by E-Mail?

I could just pass the E-Mail into the property for the username, but then things get a bit messy. The user might want to change the e-mailaddress.

Is there any resource available that decribes a good way to implement it?

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

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

发布评论

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

评论(1

谁的年少不轻狂 2024-08-01 20:40:42

您使用电子邮件地址作为用户名的方法对我来说很有效。 但你是对的; 当电子邮件地址更改时,它确实会变得混乱。 我们采取的方法是,当用户希望更改他们的电子邮件地址时,我们也会更改他们的用户名。 做到这一点的唯一方法是通过直接数据库操作。 我们非常小心地确保对用户的任何引用都是通过 UserId 完成的; 这确保了用户名更改不会破坏任何内容。

这是我们使用的代码,如果您感兴趣的话。 该代码位于一个持久类中,该类包装了 MembershipUser 类并以一对一关系存储。 只需忽略邪恶的 SQL 字符串构建并做一些更好的事情即可;)

IDbCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(
    "UPDATE dbo.aspnet_Users SET UserName = '{0}', LoweredUserName = '{0}' WHERE UserId = '{1}'",
    DatabaseManager.SqlEscape(value.ToLower()), this.Id);
cmd.ExecuteNonQuery();

cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(
    "UPDATE dbo.aspnet_Membership SET Email = '{0}', LoweredEmail = '{0}' WHERE UserId = '{1}'",
    DatabaseManager.SqlEscape(value.ToLower()), this.Id);
cmd.ExecuteNonQuery();

Your approach of using the email address as the username works well for me. But you're right; it does get messy when the email address gets changed. The approach we took was when the user wishes to change their email address, we also change their user name. The only way to do this was through direct database manipulation. We were careful to make sure that any references to the user was done through the UserId; this ensured that the user name change didn't break anything.

Here's the code we used, in case you're interested. The code is in a persistent class that sort of wraps the MembershipUser class and is stored as a one-to-one relationship. Just ignore the evil SQL string building and do something better ;)

IDbCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(
    "UPDATE dbo.aspnet_Users SET UserName = '{0}', LoweredUserName = '{0}' WHERE UserId = '{1}'",
    DatabaseManager.SqlEscape(value.ToLower()), this.Id);
cmd.ExecuteNonQuery();

cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format(
    "UPDATE dbo.aspnet_Membership SET Email = '{0}', LoweredEmail = '{0}' WHERE UserId = '{1}'",
    DatabaseManager.SqlEscape(value.ToLower()), this.Id);
cmd.ExecuteNonQuery();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文