c# - How to add custom Login and Roles mechanizm to asp mvc 5? -


i have admit. after reading quite few tutorial on new mvc 5 identity , owin stuff can't figure out. task implement login , roles listing stormpath (stormpath.com) basially web-store users , groups. have created service authenticates user & password against stormpath , returns roles/groups assigned user.

i have went applicationsigninmanager created default new mvc project in visual studio , substitutes body with:

public override task<signinstatus> passwordsigninasync(string username, string password, bool ispersistent, bool shouldlockout) {     return task.run(() =>         new stormpathservice(new configuration()).authenticateuser(username, password) != null ? signinstatus.success : signinstatus.failure);  } 

the thing passes when user inputs data login form o page, after application still thinks i'm not logged in.

what else need done asp mvc identity mechanizm respect custom way of authenticating users , roles management?

this minimum had make support logging in stormpath.

   public class applicationuser : iuser {         public string clientkey { get; set; }         public string id { get; set; }         public string username { get; set; }         public string newsfilter { get; set; }         public string fullname { get; set; }          public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager) {             // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype             var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);             // add custom user claims here             return useridentity;         }     }       public class stormpathuserstore : iuserstore<applicationuser>, iuserrolestore<applicationuser> {         private readonly istormpathservice _stormpathservice;          public stormpathuserstore(istormpathservice stormpathservice) {             if (stormpathservice == null) {                 throw new argumentnullexception("stormpathservice");             }             _stormpathservice = stormpathservice;         }          public task addtoroleasync(applicationuser user, string rolename) {             throw new notimplementedexception();         }          public task removefromroleasync(applicationuser user, string rolename) {             throw new notimplementedexception();         }          public task<ilist<string>> getrolesasync(applicationuser user) {              var groups = _stormpathservice.getusergroups(_stormpathservice.getuserurlfromid(user.id));             return task.fromresult(groups.toarray() ilist<string>);         }          public task<bool> isinroleasync(applicationuser user, string rolename) { #if debug             var configuration = objectfactory.getinstance<iconfiguration>();              if (!string.isnullorwhitespace(configuration.debuguser)) {                 return task.fromresult(configuration.debugroles.split(',').contains(rolename));             } #endif              var isingroup =                 _stormpathservice.getusergroups(_stormpathservice.getuserurlfromid(user.id)).contains(rolename);             return task.fromresult(isingroup);         }          public void dispose() {         }          public task createasync(applicationuser user) {             throw new notimplementedexception();         }          public task updateasync(applicationuser user) {             throw new notimplementedexception();         }          public task deleteasync(applicationuser user) {             throw new notimplementedexception();         }          public task<applicationuser> findbyidasync(string userid) {             var userdata = _stormpathservice.getuser(_stormpathservice.getuserurlfromid(userid));             if (userdata == null) {                 return task.fromresult((applicationuser)null);             }             var user = new applicationuser {                 username = userdata.username,                 id = userid,                 clientkey = userdata.clientid,                 newsfilter = userdata.newsfilter,                 fullname = userdata.fullname,             };             return task.fromresult(user);         }          public task<applicationuser> findbynameasync(string username) {             throw new notimplementedexception();         }     }      // configure application user manager used in application. usermanager defined in asp.net identity , used application.     public class applicationusermanager : usermanager<applicationuser> {         public applicationusermanager(iuserstore<applicationuser> store)             : base(store) {         }          public static applicationusermanager create(identityfactoryoptions<applicationusermanager> options,             iowincontext context) {             var manager =                 new applicationusermanager(new stormpathuserstore(objectfactory.getinstance<istormpathservice>()));             // configure validation logic usernames             manager.uservalidator = new uservalidator<applicationuser>(manager) {                 allowonlyalphanumericusernames = false,                 requireuniqueemail = true             };              // configure validation logic passwords             manager.passwordvalidator = new passwordvalidator {                 requiredlength = 6,                 requirenonletterordigit = true,                 requiredigit = true,                 requirelowercase = true,                 requireuppercase = true             };              // configure user lockout defaults             manager.userlockoutenabledbydefault = true;             manager.defaultaccountlockouttimespan = timespan.fromminutes(5);             manager.maxfailedaccessattemptsbeforelockout = 15;              var dataprotectionprovider = options.dataprotectionprovider;             if (dataprotectionprovider != null) {                 manager.usertokenprovider =                     new dataprotectortokenprovider<applicationuser>(dataprotectionprovider.create("asp.net identity")) {tokenlifespan = timespan.fromdays(14.0)};             }             return manager;         }     }      // configure application sign-in manager used in application.     public class applicationsigninmanager : signinmanager<applicationuser, string> {         public applicationsigninmanager(applicationusermanager usermanager, iauthenticationmanager authenticationmanager)             : base(usermanager, authenticationmanager) {         }          public override task<signinstatus> passwordsigninasync(string username, string password, bool ispersistent,             bool shouldlockout) {             return task.fromresult(                 new stormpathservice(new configuration()).authenticateuser(username, password) != null                     ? signinstatus.success                     : signinstatus.failure);         }          public override task signinasync(applicationuser user, bool ispersistent, bool rememberbrowser) {             return base.signinasync(user, true, rememberbrowser);         }          public override task<claimsidentity> createuseridentityasync(applicationuser user) {             var result = user.generateuseridentityasync((applicationusermanager) usermanager).result;             return task.fromresult(result);         }          public static applicationsigninmanager create(identityfactoryoptions<applicationsigninmanager> options,             iowincontext context) {             return new applicationsigninmanager(context.getusermanager<applicationusermanager>(), context.authentication);         }     } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -