user3595333
user3595333

Reputation: 1

ASP.NET MVC Registration create Claim on E-mail

I am new to MVC , and I would need help with creating a claim on email. I am using ASP.NET web application MVC (Visual studio 2013/Framework 5.1). My goal is to extend existing ApplicationUser model. I added [DataType(DataType.EmailAddress)] public string Email { get; set; } and created a registration model with same , updated the view so i can type in my email but I don't know how to claim it so it will be unique in database and show message like it does for username.

Upvotes: 0

Views: 85

Answers (1)

Palak Bhansali
Palak Bhansali

Reputation: 731

  • Asp.net Identity uses entity framework code first model framework by which you can generate/scaffold custom code through entity framework migrations like , Add-Migration Init, Update Database packages, which gives you add your custom code to entity framework default data model for Asp.net Identity framework.

In your case, you want to use Email instead UserName which is quite logical.

Please go through this article, though i am giving you some quick steps along with some code for your reference.

  • Enable-Migrations –EnableAutomaticMigrations - Adds Migrations folder in your project along with Configuration.cs which bootstraps automatic migrations for your db context.

  • Add-Migrations Init - This gives you new code files to customize table/properties for DbMigration class which is partial by default. It will give you two overrides Up() and Down(). For email change, here is the file which will look like below after change.

    public override void Up()
    {
        AlterColumn("dbo.AspNetUsers", "UserName", c => c.String());
    }    
    public override void Down()
    {
        AlterColumn("dbo.AspNetUsers", "UserName", c => c.String());
    }
    
  • Once you are done with this, you need to fire Update Database package from library package manager.

If everything goes well, ApplicationUser with updated dbcontext/database properties will be available to application.

Upvotes: 0

Related Questions