2013-07-01 30 views
5

Với bản phát hành ASP.NET MVC 5 Preview mới được phát hành, tôi làm cách nào để cấu hình ngữ cảnh/bảng Người dùng?Làm cách nào để định cấu hình ngữ cảnh/bảng Người dùng?

Trong MVC 4 tôi sẽ chỉ cần sử dụng lớp người dùng của riêng tôi và sau đó điểm WebSecurity khởi với nó, con chó này:

WebSecurity.InitializeDatabaseConnection(connectionString, "System.Data.SqlClient", userTableName, userIdColumn, userNameColumn, autoCreateTables); 

Tôi muốn thêm các thuộc tính bổ sung cho các lớp Users - làm thế nào?

Trả lời

1

Các lớp UserStore và User đang có để làm cho việc triển khai dựa EF dễ dàng hơn, nhưng bạn luôn có thể thả xuống và thực hiện các tùy chỉnh của riêng bạn IUserStore và vượt qua trong của riêng bạn DbContext.

Tôi có thể cung cấp ví dụ chi tiết hơn nếu bạn cần.

+0

Tôi thực sự muốn xem một ví dụ. Tôi đã cố gắng thực hiện của riêng tôi, nhưng kết thúc với một số vấn đề nhận dạng yêu cầu bồi thường, gây ra các AntiFogeryToken thất bại. Xảy ra khi tôi sử dụng Microsoft.AspNet.Identity.IUser cho lớp người dùng của riêng tôi. – Martin

+0

@Hao Kung, tôi muốn xem ví dụ trên cùng. Tôi cũng muốn tránh bảng mới cho "MyUsers". Bạn có thể vui lòng bao gồm Vai trò với các trường tùy chỉnh trong ví dụ này không? – KidCoke

+0

@Hao Kung, bạn có cung cấp ví dụ không? – RezaRahmati

1

Bạn có thể tải xuống mẫu từ https://github.com/rustd/AspnetIdentitySample. Điều này được dựa trên mẫu ASP.NET MVC được vận chuyển với ASP.NET and Web Tools 2013 Preview Refresh (Chỉ hỗ trợ phiên bản tiếng Anh của bản xem trước VS2013) Một khi bạn đã cài đặt bản xem trước làm mới này, bạn có thể làm tương tự cho các ứng dụng ASP.NET Web Forms và SPA.

Sau đây là các bước để chạy dự án này

Open the solution 
Build and run 
Register a user ---- Notice that the user registration field only has user name and password 
Let's ask for a birthdate option from the user while registering an account. 
Goto Nuget Package Manager console and run "Enable-Migrations" 
Goto Models\AppModel.cs and uncomment BirthDate property in the MyUser class 
Goto Models\AccountViewModels.cs and uncomment BirthDate property in RegisterViewModel 
Goto AccountController and in Register Action and have the following code var user = new MyUser() { UserName = model.UserName,BirthDate=model.BirthDate }; //var user = new MyUser() { UserName = model.UserName }; 
Goto Views\Account\Register.cshtml and uncomment the HTML markup to add a BirthDate column 
Goto Nuget Package Manager console and run "Add-Migration BirthDate" 
Goto Nuget Package Manager console and run "Update-Database" 
Run the application 
When you register a user then you can enter BirthDate as well 
+0

Tôi đã thấy ví dụ đó, nhưng nó tạo ra một MyUser, tôi thực sự muốn lớp người dùng của riêng tôi, không phải lớp người dùng Entity Frameworks. Vì vậy, khi sử dụng giải pháp trên, tôi nhận được bảng MyUsers trong cơ sở dữ liệu - điều mà không được mong muốn. – Martin

2

Tôi nghĩ rằng, điều này có thể giải quyết vấn đề của bạn:


Trong Models \ IdentityModels.cs bạn có thể xác định lại mô hình tài chính của bạn:

public class ApplicationUser : IdentityUser 
{ 
    /* identity field from database */ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 

    [Required] 
    public bool Internal { get; set; } 

    public string UserFullName { get; set; } 

    public string UserEmail { get; set; } 

    public ApplicationUser() 
     : base() 
    { 
     Internal = false; 
    } 

    public ApplicationUser(string userName) 
     : base(userName) 
    { 
     Internal = false; 
    } 

} 

bây giờ bạn có thể thay đổi giá trị mặc định của bản đồ Bảng AspNet sử dụng OnModelCreating() ghi đè và ToTable() methode:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 
    } 

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

     // Change the name of the table to be Users instead of AspNetUsers 
     modelBuilder.Entity<IdentityUser>().ToTable("User"); 
     modelBuilder.Entity<ApplicationUser>().ToTable("User"); 

     modelBuilder.Entity<IdentityRole>().ToTable("Role"); 
     modelBuilder.Entity<IdentityUserClaim>().ToTable("User_Claim"); 
     modelBuilder.Entity<IdentityUserLogin>().ToTable("User_Login"); 
     modelBuilder.Entity<IdentityUserRole>().ToTable("User_Role"); 
    } 
} 

Cuối cùng bạn sẽ thấy trong cơ sở dữ liệu các bảng sau: dùng, Vai trò, USER_ROLE, User_Claim, USER_LOGIN thay vì AspNetUsers, AspNetRoles, AspNetUsersRoles, AspNetUsersClaims, AspNetUserLogins.

Tất nhiên tài bảng sẽ chứa trường bổ sung: UserId (int identity), nội, UserFullNameuseremail.

Các vấn đề liên quan