Ciao a tutti,
Mi sto approcciando all'implementazione del pattern MVC con autenticazione microsoft (Direi che è ora di abbandonare webform) però ho molta confusione.
In particolare sto affrontando il problema di estendere i dati collegati agli utenti e gestire i ruoli.
Da ricerche online ho trovato le soluzioni più disparate, dalle più complicate alle più semplici.
Un approccio che ho capito poco ma sembra lineare e funzionale è questo.
Nel mio identityMoldels vado ad integrare tutte le proprietà di cui necessito.
Es :
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Tenere presente che il valore di authenticationType deve corrispondere a quello definito in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Aggiungere qui i reclami utente personalizzati
return userIdentity;
}
public string Name { set; get; }
public string Surname { set; get; }
}
Per la gestione degli utenti e dei ruoli vedo che molti creano due nuovi controller
UsersController e RolesController
Entrambe le classi vanno ad utilizzare poi ad utilizzare l'oggetto iprincipal.identity
Esempio pratico :
[Authorize]
public class RoleController : Controller
{
ApplicationDbContext context;
public RoleController()
{
context = new ApplicationDbContext();
}
/// <summary>
/// Get All Roles
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
if (!isAdminUser())
{
return RedirectToAction("Index", "Home");
}
}
else
{
return RedirectToAction("Index", "Home");
}
var Roles = context.Roles.ToList();
return View(Roles);
}
public Boolean isAdminUser()
{
if (User.Identity.IsAuthenticated)
{
var user = User.Identity;
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var s = UserManager.GetRoles(user.GetUserId());
if (s[0].ToString() == "Admin")
{
return true;
}
else
{
return false;
}
}
return false;
}
/// <summary>
/// Create a New role
/// </summary>
/// <returns></returns>
public ActionResult Create()
{
if (User.Identity.IsAuthenticated)
{
if (!isAdminUser())
{
return RedirectToAction("Index", "Home");
}
}
else
{
return RedirectToAction("Index", "Home");
}
var Role = new IdentityRole();
return View(Role);
}
/// <summary>
/// Create a New Role
/// </summary>
/// <param name="Role"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Create(IdentityRole Role)
{
if (User.Identity.IsAuthenticated)
{
if (!isAdminUser())
{
return RedirectToAction("Index", "Home");
}
}
else
{
return RedirectToAction("Index", "Home");
}
context.Roles.Add(Role);
context.SaveChanges();
return RedirectToAction("Index");
}
}
public class UsersController : Controller
{
// GET: Users
public Boolean isAdminUser()
{
if (User.Identity.IsAuthenticated)
{
var user = User.Identity;
ApplicationDbContext context = new ApplicationDbContext();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var s = UserManager.GetRoles(user.GetUserId());
if (s[0].ToString() == "Admin")
{
return true;
}
else
{
return false;
}
}
return false;
}
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
var user = User.Identity;
ViewBag.Name = user.Name;
// ApplicationDbContext context = new ApplicationDbContext();
// var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
//var s= UserManager.GetRoles(user.GetUserId());
ViewBag.displayMenu = "No";
if (isAdminUser())
{
ViewBag.displayMenu = "Yes";
}
return View();
}
else
{
ViewBag.Name = "Not Logged IN";
}
return View();
}
}
Quello che vedo mi sembra abbastanza logico, non capisco perchè utilizzare la classe user, non c'è già il controller Account di microsoft? non potrei estendere i metodi li?
Perchè è stato implementato lo stesso metodo isAdminUSer da una parte e dall'altra a che serve?
Voi cosa ne pensate è corretto questo approccio?
Avete da consigliarmi guide, approcci, video o qualsiasi altra cosa su cui mi potrei formare adeguatamente?
Grazie mille a tutti.