locked
Identity without email verification RRS feed

  • Question

  • User511720757 posted

    Hi!

    Question
    Can I use ASP.NET Identity on localhost, without using the email verification?

    Background
    I read that Identity uses email authentication for user registration. But I don't think my localhost are allowed to send emails.
    I ask because I am thinking of learning Identity, and use ut on my localhost computer.

    Saturday, January 23, 2016 1:12 PM

Answers

  • User614698185 posted

    Hi olof84,

    ASP.NET Identity can be set in App_Start\IdentityConfig.cs:

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        ...
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser> (context.Get<ApplicationDbContext>()));
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                // This disables the validation check on email addresses
                RequireUniqueEmail = false
            };
        }
    }

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 7:50 AM

All replies

  • User1124521738 posted

    yes, you can, depending on which provider framework (sql membership provider, owin etc) you are using, you may need to do some configuration to the identity to disregard the check.

    note: password recovery/reset often requires email to be working, so keep that in mind.

    consider for localhost (local development) to set to use pickup folder rather than sending email - you'll get EML files generated based on the email that would have been sent.

        <system.net>
            <mailSettings>
                <smtp from="web@example.com" deliveryMethod="SpecifiedPickupDirectory">
                    <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
                </smtp>
            </mailSettings>
        </system.net>

    Sunday, January 24, 2016 1:07 AM
  • User614698185 posted

    Hi olof84,

    ASP.NET Identity can be set in App_Start\IdentityConfig.cs:

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        ...
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser> (context.Get<ApplicationDbContext>()));
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                // This disables the validation check on email addresses
                RequireUniqueEmail = false
            };
        }
    }

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 7:50 AM
  • User-15204943 posted

    I have tried numerous things that *seem* to work.  However, I still cannot get around the validator in the login box.  That setting does NOT disable the validator, it only disables checking for duplicate email addresses.  Attempting to login without "name@address.tld" results in the "The Login field is not a valid e-mail address." error, and no attempt is made to check the UserName at all. 

    I cannot find a way to turn that off.

    It looks like I may have to abandon ASP.NET Identity entirely, and use something else.

    Friday, January 29, 2016 5:26 PM
  • User1124521738 posted

    the default generated code from a new project will encourage use of email address as synonymous with username, but with a little work, it can have the username attribute substituted in and managed separately from email.  This is independent of the Identity, and is only a minor configuration/implementation change against the default auto generated code from Visual Studio.

    the email attribute of the logon model is hardcoded to validate that it is an email pattern, but you can swap the expected email for the actual username, if the model only has Email, add Username or change Email to Username.  Just also note that some of the attributes should be modified so that it will stop requiring @ signs etc such as found in email addresses.

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    [StringLength(128)]
    public string Email { get; set; }
    
    [Required]
    [Display(Name = "Username")]
    [StringLength(128)]
    public string UserName { get; set; }

    Then change in the view from @Html.TextBoxFor(m => m.Email) to use @Html.TextBoxFor(m => m.UserName) in your login view, also update any validations.

    For reference, see https://github.com/ninianne98/CarrotCakeCMS-MVC/blob/72ca5014dabfd1fbebbfae68e800efbb0675198c/CMSSecurity/AccountViewModels.cs and https://github.com/ninianne98/CarrotCakeCMS-MVC/blob/72ca5014dabfd1fbebbfae68e800efbb0675198c/CMSAdmin/Views/CmsAdmin/Login.cshtml and https://github.com/ninianne98/CarrotCakeCMS-MVC/blob/72ca5014dabfd1fbebbfae68e800efbb0675198c/CMSAdmin/Controllers/CmsAdminController.cs method "public async Task<ActionResult> Login(LoginViewModel model)"

    Friday, January 29, 2016 6:44 PM