Asp.net Identity 2.0 作弊条

Moving ASP.NET Identity model to class library

http://stackoverflow.com/questions/23446919/moving-asp-net-identity-model-to-class-library

To move the IdentityModel into a class library (which is the right thing to do according to the SRP), follow these steps:

  1. Create a class library. (ClassLibrary1)
  2. Using NuGet, add a reference to Microsoft.AspNet.Identity.EntityFramework. This will also auto-add some other references.
  3. Add a reference in your website to ClassLibrary1
  4. Find WebSite/Models/IdentityModel.cs and move it to ClassLibrary1.
  5. Make IdentityModel.cs look like this:

    public class ApplicationUser : IdentityUser
    {
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("YourContextName")
        {
        }
    }
  6. Make sure your website's Web.config has YourContextName pointing to the right database in the section. (Note: this database can and should house your application data).

    <add name="YourContextName" connectionString="YourConnectionStringGoesHere"
      providerName="System.Data.SqlClient" />
  7. Make your EF Context class inherit from your ApplicationDbContext:

    public class YourContextName : ApplicationDbContext
    {
        public DbSet<ABizClass1> BizClass1 { get; set; }
        public DbSet<ABizClass2> BizClass2 { get; set; }
        // And so forth ...
    }

     

When anyone in your site tries to log in or register, the Identity system will route them to your database with all your data which includes the Identity tables.

 

Use Username instead of Email for identity

http://stackoverflow.com/questions/27501533/use-username-instead-of-email-for-identity-in-asp-net-mvc5

In case anyone else gets here from a google search it is actually very easy to do this. If you look at the register post action it is simply setting the UserName to the Email address. So.........

Add UserName to the RegisterViewModel and add it to the register view.

<div class="form-group">
        @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "Username or email" })
        </div>
</div>

In the Register Post Action on the AccountController set the UserName to the ViewModel's UserName and the Email to the ViewModel's Email.

var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };

 

Change AspNet Identity table names 

http://stackoverflow.com/questions/19460386/how-can-i-change-the-table-names-when-using-visual-studio-2013-aspnet-identity

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder); // This needs to go before the other rules!

    modelBuilder.Entity<ApplicationUser>().ToTable("User");
    modelBuilder.Entity<IdentityRole>().ToTable("Role");
    modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
    modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
    modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
}

 

Make email as non unique

http://stackoverflow.com/questions/27501533/use-username-instead-of-email-for-identity-in-asp-net-mvc5

Configure below in IdentityConfig.cs

manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
     AllowOnlyAlphanumericUserNames = false,
     RequireUniqueEmail = false                
};

 

Change password rules

Configure in IdentityConfig.cs

PasswordValidator = new PasswordValidator
{
    RequireDigit = false,
    RequireLowercase = false,
    RequireNonLetterOrDigit = false,
    RequireUppercase = false,
    RequiredLength = 2
};

 

转载于:https://www.cnblogs.com/heuyang/p/5709734.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值