2015-06-03 91 views
3

我是一名Asp.net开发人员,但对Asp.net身份框架非常陌生。我一直在研究示例应用程序,并在Identity上也使用了一些教程,但我仍然无法完全理解这个概念。我非常坚信Asp.net会员身份,但身份似乎没有像会员资格。我会解释我到目前为止所做的。了解Asp.Net身份关键点

我正在创建一个简单的应用程序,其中我遵循代码第一种方法。我为UserUser创建了实体模型,并且有一些额外的字段。以下是用户的实体模型。

public class User : IdentityUser 
{ 
    public int? CompanyID { get; set; } 

    public bool? CanWork { get; set; } 

    public bool? CanSearch { get; set; } 

    public Company Company { get; set; } 
} 

现在在示例中,人们使用名称ApplicationUser,但为了我自己的目的,我使用了名称User。用户或应用程序用户模式中有一种方法,即:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager) 
    { 
     CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 

我无法理解此方法的用途。也从一个例子,我已经使用了角色如下模型,

public class Role : IdentityRole 
{ 
    public Role() 
    { 

    } 

    public Role(string roleName, string description) 
     : base(roleName) 
    { 
     this.Description = description; 
    } 

    public string Description { get; set; } 
} 

我明白,一个额外的字段添加,但我无法理解重载的构造的目的。

上述混淆是次要的。我的主要困惑是,我很熟悉,当我创建实体模型时,我使用DbSet和DbContext,当我调用任何实体框架方法来访问数据库时,无论我遵循何种方案创建/删除数据库。

在Identity中哪个方法负责在数据库中创建Identity表?我有一个IdentityConfig文件,我在其中声明ApplicationUserManager和ApplicationSignInManager。我也有一个启动文件。以前,我在App_Start文件夹中只有一个Startup文件,当我运行应用程序并尝试访问任何Identity方法时,它给了我错误并且没有创建数据库。然后,我将该类作为partial,并在根上创建了另一个具有相同名称的分部类,然后异常消失并创建了表。所以Startup类负责创建标识表?在AspNetUsers中会自动创建额外的列,如PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled。我不需要这些额外的列。我可以删除这些吗?我可以更改创建的标识表的名称吗?

我知道这些都是非常基本的问题,根本不是一个问题,但如果我无法为初学者找到一些基本的教程或例子,那么这将是非常有益的。我发现的是描述那些我不需要或者让我迷惑的东西。我想了解并控制身份应该如何在我的应用程序中工作,但直到现在,在我看来,我并没有完全掌握它,也无法根据我的需要进行调整。它的教程和示例正在教我如何制作句子,但我无法理解字母。 :(

回答

8

首先,你必须定义模型的 - 因为你正在做的 - 实施正确的接口
比方说,你要创建一个用户,您的应用程序:

public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim> 
{ 
    public string CompanyName { get; set; } 
} 

正如你可以看到我已经实现了IdentityUser接口(命名空间Microsoft.AspNet.Identity.EntityFramework)。

我指定我想用我的主键(串)什么类型的标识符和包括我的自定义对象管理登录,角色a索赔。

现在,我们可以定义角色对象:

public class MyRole : IdentityRole<string, MyUserRole> 
{ 
} 

同样有一个类型,我属于一个角色的用户管理定义的类。

public class MyUserRole : IdentityUserRole<string> 
{ 
} 

MyUserLogin是要实现IdentityUserLogin<string>
MyUserClaim即将执行IdentityUserClaim<string>

正如你所看到的,每个接口都需要一个主键类型。

第二步是建立用户存储:

public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim> 
{ 
    public MyUserStore(MyContext context) 
     : base(context) 
    { 
    } 
} 

我们再次定义了什么用户,角色,登录等等等等,我们要使用。
我们需要UserStore导致我们的UserManager需要一个。

如果您打算管理角色并将角色与每个用户关联,您必须创建您的RoleStore定义。

public class MyRoleStore : RoleStore<MyRole, string, MyUserRole> 
{ 
    public DaufRoleStore(ApplicationDatabaseContext context) : base(context) 
    { 
    } 
} 

现在您可以创建UserManager。 UserManager是保存UserStore更改的真实responsible

public class ApplicationUserManager : UserManager<MyUser, string> 
{ 
    public ApplicationUserManager(IUserStore<MyUser, string> store) 
     : base(store) 
    { 

    } 

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>())); 

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

     manager.PasswordValidator = new PasswordValidator() 
     { 
     RequiredLength = 5, 
     RequireNonLetterOrDigit = false,  // true 
     // RequireDigit = true, 
     RequireLowercase = false, 
     RequireUppercase = false, 
     }; 

     return (manager); 
    } 
} 

该类有一个静态方法,它将为您创建一个新的UserManager。
有趣的是,你可以包括一些验证规则,你可能需要验证密码等。

最后一件事是创建或数据库上下文。

public class MyContext : IdentityDbContext<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim> 
{ 
    public MyContext(): base("<your connection string here>") 
    { 

    } 

    public static MyContext Create() 
    { 
     return new MyContext(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     modelBuilder.Entity<MyUser>() 
      .ToTable("Users"); 

     modelBuilder.Entity<MyRole>() 
      .ToTable("Roles"); 

     modelBuilder.Entity<MyUserRole>() 
      .ToTable("UserRoles"); 

     modelBuilder.Entity<MyUserClaim>() 
      .ToTable("UserClaims"); 

     modelBuilder.Entity<MyUserLogin>() 
      .ToTable("UserLogins"); 
    } 
} 

正如您所看到的,我已经使用模型构建器来更改所有表的名称。 您可以在此处定义键或字段类型或表关系。

这是你要附上你想在你的背景来管理您的自定义类的地方:

public DbSet<MyCustomer> Customers{ get; set; } 

再次MyContextCreate方法返回一个新的上下文:

public static MyContext Create() 
{ 
    return new MyContext(); 
} 

现在你应该有一个启动课,你要引导你的东西:

[assembly: OwinStartup(typeof(ASPNETIdentity2.Startup))] 

namespace ASPNETIdentity2 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.CreatePerOwinContext(MyContext.Create); 
      app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
     } 
    } 
} 

这里您将创建您的数据库上下文以及您可以在应用程序中使用的用户管理器。

通知的第一行:

[assembly: OwinStartup(typeof(ASPNETIdentity2.Startup))] 

原因你告诉你的环境是启动类需要在...启动被称为这是必要的。

现在在你的控制器,你可以简单地参考您的UserManager做这样的事情:

HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 

你怎么能创建表?

在Visual Studio中,转至工具 - > NuGet Packager Manager - >程序包管理器控制台。

在窗口中有一个组合框“默认项目”。选择你的ASP.NET MVC项目。
运行以下命令:

Enable-Migrations 

它会在一个名为Migrations新文件夹创建一个文件Configuration.cs
如果你想创建你的数据库,你需要打开文件并更改AutomaticMigrationsEnabled为true:

public Configuration() 
{ 
    AutomaticMigrationsEnabled = true; 
} 

再次,从Package Manager Console,你可以运行:

Update-Database 

和所有的表会出现在你的数据库中。不要忘记你的连接字符串。

你可以下载这个github project看看一切正常。
您可以使用其他信息检查这些twoanswers

两者的first已获得一些链接到一个博客,你可以了解所有这些事情。

注:

你要做到这一切,如果你想定制你的环境的每一个位。

+0

哇。谢谢。你所放的细节是可以理解的,我真的很感谢你的努力。它很简洁,但完全描述了所有的事情。祝你有美好的一天。 –

+2

很高兴我以某种方式提供帮助。祝你好运。我不是特别喜欢所有这些烂摊子,但这是它的工作方式。干杯。 – LeftyX