Posted on Lascia un commento

ASP.NET MVC : GESTIONE UTENTE E RUOLI E ACCESSO

Nelle lezioni precedenti abbiamo visto le tabelle create dall’Entity Framework per gestire utenti e ruoli. Avete registrato un utente ed eseguito un login. Facciamo qualche personalizzazione.

Regole delle password.

Potete cambiare tali regole nel file IdentityConfig.cs nella directory App_Start. Nella funzione Create della classe ApplicationUserManager, trovate :

//REGOLE DI COSTRUZIONE PASSWORD
 manager.PasswordValidator = new PasswordValidator
 {
     RequiredLength = 6,
     RequireNonLetterOrDigit = false,    //RequireNonLetterOrDigit = true,
     RequireDigit = false, 
//RequireDigit = true,
     RequireLowercase = false, 
//RequireLowercase = true,
     RequireUppercase = false, 
//RequireUppercase = true,
 };

Potete vedere un impostazione meno restrittiva delle password. Commentati vi vedete i valori di sicurezza di default.


Inserimento di Ruoli e Utenti via codice.

Il nostro obbiettivo e quello che il programma crei per noi i ruoli che andremo a gestire nel database e anche un utente “nostro”, senza dover effettuare una prima registrazione.:un nostro utente amministratore creato in automatico.

Nel file Startup.cs troviamo una classe eseguita alla partenza dell’applicazione.

public partial class Startup
 {
    public void Configuration(IAppBuilder app)
   {
        ConfigureAuth(app);
   }
 }

In questa funzione, inseriamo anche la chiamata ad una nostra funzione createRolesandUsers();

private void createRolesandUsers()
 {
 ApplicationDbContext context = new ApplicationDbContext();
 
 var roleManager = new RoleManager<IdentityRole>(new     RoleStore<IdentityRole>(context));
 var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

// In Startup iam creating first Admin Role and creating a default Admin User
 if (!roleManager.RoleExists("Admin"))
 {

// first we create Admin rool
 var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
 role.Name = "Admin";
 roleManager.Create(role);

//Here we create a Admin super user who will maintain the website 
 var user = new ApplicationUser();
 user.UserName = "myUser";
 user.Email = "myuser@mcontoso.com";

string userPWD = "123456789";
 var chkUser = UserManager.Create(user, userPWD);

//Add default User to Role Admin
 if (chkUser.Succeeded)
 {
 var result1 = UserManager.AddToRole(user.Id, "Admin");
 }
 }
 // creating Creating Manager role
 if (!roleManager.RoleExists("Teacher"))
 {
    var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
   role.Name = "Teacher";
 roleManager.Create(role);
 }

// creating Creating Employee role
 if (!roleManager.RoleExists("Student"))
 {
 var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
 role.Name = "Student";
 roleManager.Create(role);
 }
 }

Lanciate l’applicazione e vedrete che potrete loggarvi con l’utente creato via codice.

Per una trattazione molto più dettagliata consiglio l’ottimo articolo di Syed Shanu su Code Project

 

 

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *