Yes, you can. Parts of authentication and authorization work independently. If you have your own authentication service, you can simply use the OWIN authorization part. You already have a UserManager that checks for username and password . Therefore, you can write the following code in your action after entering your account:
[HttpPost] public ActionResult Login(string username, string password) { if (new UserManager().IsValid(username, password)) { 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), // optionally you could add roles if any new Claim(ClaimTypes.Role, "RoleName"), new Claim(ClaimTypes.Role, "AnotherRole"), }, 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 your user manager might be something like this:
class UserManager { public bool IsValid(string username, string password) { using(var db=new MyDbContext())
In the end, you can protect your actions or controllers by adding the Authorize attribute.
[Authorize] public ActionResult MySecretAction() {
Sam Farajpour Ghamari Jul 23 '15 at 11:16 2015-07-23 11:16
source share