You can use Microsoft Identity for this kind of advanced scenario. The identity is so modular that you can use any data warehouse with any necessary scheme. Identity authentication password is not needed, you can implement your own script. consider this simple example:
// imaging this action is called after user authorized by remote server public ActionResoult Login() { // imaging this method gets authorized certificate string // from Request.ClientCertificate or even a remote server var userCer=_certificateManager.GetCertificateString(); // you have own user manager which returns user by certificate string var user=_myUserManager.GetUserByCertificate(userCer); if(user!=null) { // user is valid, going to authenticate user for my App var ident = new ClaimsIdentity( new[] { // since userCer is unique for each user we could easily // use it as a claim. If not use user table ID new Claim("Certificate", userCer), // adding following 2 claim just for supporting default antiforgery provider new Claim(ClaimTypes.NameIdentifier, userCer), new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"), // an optional claim you could omit this new Claim(ClaimTypes.Name, user.Name), // populate assigned user role form your DB // and add each one as a claim new Claim(ClaimTypes.Role, user.Roles[0].Name), new Claim(ClaimTypes.Role, user.Roles[1].Name), // and so on }, DefaultAuthenticationTypes.ApplicationCookie); // Identity is sign in user based on claim don't matter // how you generated it Identity take care of it HttpContext.GetOwinContext().Authentication.SignIn( new AuthenticationProperties { IsPersistent = false }, ident); // auth is succeed, without needing any password just claim based return RedirectToAction("MyAction"); } // invalid certificate ModelState.AddModelError("", "We could not authorize you :("); return View(); }
As you can see, we authorized the user and filled roles without any dependence on the username, password and any data store, since we used our own user manager.
Usage example:
[Authorize] public ActionResult Foo() { } // since we injected user roles to Identity we could do this as well [Authorize(Roles="admin")] public ActionResult Foo() { // since we injected our authentication mechanism to Identity pipeline // we have access current user principal by calling also // HttpContext.User }
This is a simple example of how you can implement your own advanced IIdenity script. Read my other similar answers, for example, and for more examples to see how you can do almost everything: Claims .
You can also view and download the Token Based Authentication Example as a simple working example.
Sam Farajpour Ghamari
source share