2013-07-01 12 views
5

Con la nuova anteprima ASP.NET MVC 5 rilasciata, come configurare il contesto/tabella Utenti?Come si configura il contesto/tabella Utenti?

In MVC 4 Vorrei solo usare la mia classe User e quindi puntare il WebSecurity inizializzare ad esso, Tike questo:

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

desidero aggiungere ulteriori proprietà alla classe degli utenti - come?

risposta

1

Le classi UserStore e Utente sono lì per rendere implementazioni basate EF più facile, ma si può sempre cadere giù e mettere in atto il proprio personalizzato IUserStore e passare il proprio DbContext.

Posso fornire un esempio più dettagliato se necessario.

+0

Mi piacerebbe molto vedere un esempio. Ho provato a implementare il mio, ma ho avuto alcuni problemi di identità di reclamo, causando il fallimento di AntiFogeryToken. Accade quando utilizzo Microsoft.AspNet.Identity.IUser per la mia classe utente. – Martin

+0

@Hao Kung, mi piacerebbe vedere un esempio dall'alto verso il basso. Voglio anche evitare la nuova tabella per "MyUsers". Potresti per favore includere ruoli con campi personalizzati nell'esempio? – KidCoke

+0

@Hao Kung, forniresti un esempio? – RezaRahmati

1

È possibile scaricare un campione da https://github.com/rustd/AspnetIdentitySample. Questo è basato sul modello ASP.NET MVC fornito con ASP.NET and Web Tools 2013 Preview Refresh (Supporta solo la versione inglese dell'anteprima VS2013) Una volta installato questo aggiornamento dell'anteprima, è possibile fare lo stesso per i moduli Web ASP.NET e le applicazioni SPA.

Di seguito sono riportati i passaggi per eseguire questo progetto

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

Ho già visto questo esempio, ma crea un MyUser, voglio davvero la mia classe utente, non la classe utente Entity Frameworks. Quindi, quando si utilizza la soluzione di cui sopra ottengo una tabella MyUsers nel database - che non è voluto. – Martin

2

penso, questo può risolvere il problema:


In modelle \ IdentityModels.cs è possibile ridefinire il proprio modello User:

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; 
    } 

} 

ora è possibile modificare la mappatura dei default Tabelle AspNet che utilizzano OnModelCreating() overridding e ToTable() metodo:

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"); 
    } 
} 

Infine si vedrà nel database seguenti tabelle: utente, Ruolo, USER_ROLE, User_Claim, user_login invece di AspNetUsers, AspNetRoles, AspNetUsersRoles, AspNetUsersClaims, AspNetUserLogins.

Naturalmente il utente tabella contiene campi aggiuntivi: UserId (int identità), interno, UserFullName e UserEmail.

Problemi correlati