Francesco Bigi

Francesco Bigi

  • 1.7k
  • 57
  • 82k

How to set rules on AspNetRules?

Jan 25 2017 7:06 AM
Hello Guys,
I am bit confused on how to set rules as "Admin" "client" "normal user" on AspNetRules
What I have done is:
1. I created a MVC project
2. Made a connection string with my  local db sql server management studio
3. I executed the application, and created the first user, in order to create all the tables on my db. As you can see below
 
What I have tried to on Controllers --> AccountController.cs
 
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task Register(RegisterViewModel model)
{
if (!ModelState.IsValid) return View(model);
var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
try
{
using (var dal = new DALEF.WebShopEntities())
{
dal.Client.Add(
new DALEF.Client{
CLI_Nom = model.Nom,
CLI_Prenom = model.Prenom,
CLI_Civilite = model.Civilite,
CLI_Email = model.Email,
CLI_Adresse = model.Adresse,
CLI_CodePostal = model.CodePostal,
CLI_Ville = model.Ville,
CLI_Telephone = model.Telephone,
CLI_AspUser_Id = user.Id
});
dal.SaveChanges();
}
}
catch (Exception)
{
// client n'est pas ajouté delete l'asp user créé
ModelState.AddModelError("", "Echec création client, veuillez réessayer");
return View(model);
}
//await UserManager.AddToRoleAsync(user.Id, "client");
await SignInAsync(user, isPersistent:false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
 
 
But when I create a new user, I receive an error:
When I check the db, i see that the user is created, but not the rule. 
 
 How can I create rules? Just very simple rules as "Admin" or "Normal user", in order to create the [Authorize] for the Views.

Thanks in advance.

Answers (1)