Configure Microsoft.AspNet.Identity to allow email address as username

前端 未结 13 1978
时光取名叫无心
时光取名叫无心 2020-11-28 20:08

I\'m in the process of creating a new application and started out using EF6-rc1, Microsoft.AspNet.Identity.Core 1.0.0-rc1, Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc

相关标签:
13条回答
  • 2020-11-28 20:45

    If you are using ASP.Net webforms and are trying to accomplish this, simply open up your IdentityModels.vb/cs file and under Public Class UserManager, have it look as so:

    Public Class UserManager
    Inherits UserManager(Of ApplicationUser)
    
    Public Sub New()
        MyBase.New(New UserStore(Of ApplicationUser)(New ApplicationDbContext()))
        Users = store
        UserValidator = New UserValidator(Of ApplicationUser)(Me) With {.AllowOnlyAlphanumericUserNames = False}
    End Sub
    
    Public Property Users() As IUserStore(Of ApplicationUser)
        Get
            Return m_Users
        End Get
        Private Set(value As IUserStore(Of ApplicationUser))
            m_Users = value
        End Set
    End Property
    Private m_Users As IUserStore(Of ApplicationUser)
    
    End Class
    
    0 讨论(0)
  • 2020-11-28 20:46

    You can allow this by plugging in your own UserValidator on the UserManager, or just by turning it off on the default implementation:

    UserManager.UserValidator = new UserValidator<TUser>(UserManager) { AllowOnlyAlphanumericUserNames = false }
    
    0 讨论(0)
  • 2020-11-28 20:50

    In my case i had a repository class working with authentication, that didn't let me use a "-" inside usernames.. The fix was inside the constructor here:

    //-------------------------------------------------------
    public AuthRepository()
    //-------------------------------------------------------
    {
        _ctx = new AuthContext();
        _userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(_ctx));
        _userManager.UserValidator = new UserValidator<IdentityUser>(_userManager)
        {
            AllowOnlyAlphanumericUserNames = false
        };
    }
    
    0 讨论(0)
  • 2020-11-28 20:50

    If you are using IOC of some sort ( I am using StructureMap) in your account controller, you will need to apply the fix mentioned above by Hao Kung when the Usermanager is passed in: (I had to). There may be a way to do it in the IOC setup, but I don't know how.

    public AccountController(ApplicationUserManager userManager)
        {
            _userManager = userManager;
            _userManager.UserValidator = new UserValidator<ApplicationUser>(_userManager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
    
    0 讨论(0)
  • 2020-11-28 20:51

    If you cannot find IdentityConfig.cs then replace your AccountController constructor with this code.

    public AccountController(UserManager<ApplicationUser> userManager)
    {
    UserManager = userManager;
    UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) 
      {
          AllowOnlyAlphanumericUserNames = false  
      };
    }
    
    0 讨论(0)
  • 2020-11-28 20:51

    As you will have probably found out (and was to be expected), ASP.NET Identity 2.0.0, released March 2014, adds this functionality in the framework.

    Announcement: http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx

    Full example and tutorial, including account confirmation: http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity

    0 讨论(0)
提交回复
热议问题