0

I am working on a project that uses ASP.Net Identity 2.0. My project is based on ASP.Net 4.5 C# Web Application.

I want to change config of Identity, so that I can use email address instead of username. but when I try to register a record, the error shows to me that you can only use Alphanumeric for username.

I made the below changes to IdentityModels.cs in my C# web application but it doesn't work:

public UserManager()
   : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator<ApplicationUser>(this) {
                AllowOnlyAlphanumericUserNames = false };
    }

What should I do?

Please help me!

Amir Aria
  • 61
  • 1
  • 5

1 Answers1

0

First change you code as follows.

   public UserManager()
   : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator<ApplicationUser>(this) {
                AllowOnlyAlphanumericUserNames = true,
                RequireUniqueEmail = true };
    }

If that did not work use below method

If you are using asp.net web application with individual account template using visual studio 2013 update 3 or later, then this functionality already in there.

There are two ways you can achieve this.

  1. Create new web application chose asp.net web application, then select MVC and select individual accounts using change authentication button.

Then you will get created out of the box asp.net identity configuration classes.

Check the IdentityConfig.cs file inside the App_Start folder. You can see that there are number of identity configuration included in that file.

// Configure the application user manager which is used in this application.
    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
            IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }

You can see the comment of Configure validation logic for usernames the use of emails for userName already configured.

Also check the Startup.Auth.cs file, and you can see that the owin start up configurations included in that file to run per web request.

// Configure the db context, user manager and signin manager to use a single instance per request.

        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

Change your application to support that configuration setting to easili get this work, you don't need to do any database changes.

DSR
  • 4,588
  • 29
  • 28
  • Thanks for your solutions! I checked ASP.Net Identity in my C# project and I noticed it was Version 1.0 (by default). So, I installed ASP.Net Indetity version 2.0 and it did work for me. – Amir Aria Feb 16 '15 at 11:05