Of course, Identity so powerful and flexible that you can customize it. Use your user right as a claim, and then write an individual AuthorizeAttribute to check the claim, for example, consider this code:
[HttpPost] public ActionResult Login(string username, string password) { if (_userManager.IsValid(username, password)) // your own user manager { var ident = new ClaimsIdentity( new[] { // adding following 2 claim just for supporting default antiforgery provider new Claim(ClaimTypes.NameIdentifier, username), new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"), new Claim(ClaimTypes.Name, username), // populate assigned user rightID form the DB and add each one as a claim new Claim("UserRight","FirstAssignedUserRightID"), new Claim("UserRight","SecondAssignedUserRightID"), }, DefaultAuthenticationTypes.ApplicationCookie); HttpContext.GetOwinContext().Authentication.SignIn( new AuthenticationProperties { IsPersistent = false }, ident); return RedirectToAction("MyAction"); // auth succeed } // invalid username or password ModelState.AddModelError("", "invalid username or password"); return View(); }
And write an authorization attribute based on the claim:
public class ClaimsAccessAttribute : AuthorizeAttribute {
In the end, you just need to add your attribute to your actions:
[ClaimsAccess(CliamType="UserRight",Value="YourRightID"] public ActionResult MyAction() {
I skipped the user group to simplify this example, and I also hard-coded some parts that you need to write to the provider to retrieve from the database.
Sam Farajpour Ghamari Jul 31 '15 at 21:10 2015-07-31 21:10
source share