将电子邮件地址设置为 ASP.NET 会员提供商中的用户名

发布于 2024-10-28 07:49:40 字数 94 浏览 1 评论 0原文

我想在会员 api 中使用电子邮件地址作为用户名,而不是接受用户名。 我希望用户可以使用电子邮件地址注册我的网站,并且可以使用电子邮件 ID 和密码而不是用户名和密码登录。

I want to use email address as username in membership api instead of accepting a username.
I want that a user can signup to my site using email address and he can login using email id and password instead username and password.

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

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

发布评论

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

评论(3

酷炫老祖宗 2024-11-04 07:49:40

这就是我们所做的,因此它是可重用的,我们可以在 web.config 中注册它。如果你仔细想想,这就是最重要的地方。只要您的验证和前端向最终用户表明他们的用户名应该是电子邮件,并且您进行了正确的验证...从那里它会正常调用支持您的任何会员提供商。快捷版本的新方法,并将原始版本隐藏/更改在我们的外观后面。

public class EmailAsUsernameMembershipProvider : SqlMembershipProvider
{
    public MembershipUser CreateUser(string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return base.CreateUser(email, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }
    private new MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return base.CreateUser(email, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }

    public override bool RequiresUniqueEmail
    {
        get
        {
            return true;
        }
    }
}

This is what we did, so its reusable and we can reg it in the web.config. If you think about it, this is where it counts. as long as your validation and frontend indicate to the enduser that their username should be email, and you do proper validation... from there its normal calls to whatever membership provider is backing you. New method for the shortcut version, and hiding/altering the original behind our facade.

public class EmailAsUsernameMembershipProvider : SqlMembershipProvider
{
    public MembershipUser CreateUser(string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return base.CreateUser(email, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }
    private new MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return base.CreateUser(email, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }

    public override bool RequiresUniqueEmail
    {
        get
        {
            return true;
        }
    }
}
又怨 2024-11-04 07:49:40

正如本线程中所描述的,这一切都是可能的,但需要注意一件事:在数据库中,用户名的长度为 50 个字符(表 Users),而电子邮件(表 Memberships)的长度为 256 个字符。事实上,这意味着更短的电子邮件地址。 :)

This is all possible as described in this thread already, but there's one thing to be aware of: in the database the username is 50 chars (table Users) long while the email (table Memberships) is 256 chars long. In fact that means shorter email addresses. :)

眼睛会笑 2024-11-04 07:49:40

只需强制用户名=用户电子邮件即可。

为此,只需在用户创建表单上放置一个正则表达式验证器,以仅允许正确的电子邮件作为用户名。

根据用户的电子邮件重命名现有的用户名。

Just force user name = user email.

To do this just put a RegEx validator on user creation form to allow only proper email as user name.

Rename already existing user names according to users' email.

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