Wednesday, 15 July 2015

c# - MVC5: UserManager.AddToRole(): "Error Adding User to Role: UserId not found"? -



c# - MVC5: UserManager.AddToRole(): "Error Adding User to Role: UserId not found"? -

i have been experimenting mvc5/ef6 , trying out new identity authentication code-first migrations. in solution building , can add together migration, when perform update-database through package manager console in vs2013, configuration.cs file fails process test info tables , outputs error adding user role: userid not found.

i have tried explicitly setting user id , leaving generated manager (as seen in examples), each time receive same error message. know error failing in #region user & user roles of configuration.cs file, i'm not sure why:

using microsoft.aspnet.identity; using microsoft.aspnet.identity.entityframework; using personalportfolio2.helper; using personalportfolio2.models; using system; using system.collections.generic; using system.data.entity; using system.data.entity.migrations; using system.diagnostics; using system.linq; namespace personalportfolio2.models { public sealed class configuration : dbmigrationsconfiguration<personalportfolio2.models.applicationdbcontext> { public configuration() { automaticmigrationsenabled = false; } protected override void seed(personalportfolio2.models.applicationdbcontext context) { blobhelper bh = new blobhelper(); //locationhelper lh = new locationhelper(); applicationdbcontext db = new applicationdbcontext(); #region roles seek { list<string> myroles = new list<string>(new string[] { "root", "admin", "outsider", "client", "primary" }); var rolemanager = new rolemanager<identityrole>(new rolestore<identityrole>(context)); foreach (string r in myroles) { rolemanager.create(new identityrole(r)); } } grab (exception ex) { throw new exception("error create roles: " + ex.message); } #endregion #region user & user roles var store = new userstore<applicationuser>(context); var manager = new usermanager<applicationuser>(store); list<applicationuser> myusers = gettestusers(); var passwordhasher = new passwordhasher(); foreach (var u in myusers) { var userexists = db.users.where(a => a.email == u.email).firstordefault(); if (userexists == null) { var user = new applicationuser { email = u.email, passwordhash = passwordhasher.hashpassword("p@ssword1"), lockoutenabled = false, name = u.name, position = u.position, registereddate = datetime.now, lastvisitdate = datetime.now, organizationid = u.organizationid, profilepicturesrc = u.profilepicturesrc, }; seek { var usercreateresult = manager.create(user); } grab (exception ex) { throw new exception("error add together user: " + ex.message + "\n" + ex.innerexception); } // add together user roles list<string> usersroles = getuserroles(u.email); bool codehit = false; foreach (string role in usersroles) { seek { codehit = true; manager.addtorole(user.id, role); } grab (exception ex) { // error! throw new exception("error adding user role: " + ex.message + "\n" + ex.data + "\n" + ex.innerexception + "\nname: " + user.name + "\nemail: " + user.email + "\nuser.id: " + user.id + "\nu.id: " + u.id + "\nrole: " + role + "\ncodehit: " + codehit); } } } } #endregion } #region helpers private list<applicationuser> gettestusers() { list<applicationuser> testusers = new list<applicationuser> { new applicationuser { id = "1", email = "admin@abc.com", name = "admin user", registereddate = system.datetime.now, lastvisitdate = system.datetime.now, position = "site administrator", phonenumber = "1234564321", }, new applicationuser { id = "2", email = "first.last@hotmail.com", name = "james woods", registereddate = system.datetime.now, lastvisitdate = system.datetime.now, position = "software developer / web designer", phonenumber = "1234567890", }, new applicationuser { id = "3", email = "tyler.perry@gmail.com", name = "tyler perry", registereddate = system.datetime.now, lastvisitdate = system.datetime.now, position = "company contact", phonenumber = "1234567890", } }; homecoming testusers; } public list<string> getuserroles(string user) { list<string> myroles = new list<string>(); switch (user) { //"root", "admin", "outsider", "client", "primary" case "admin@abc.com": myroles = new list<string>(new string[] { "root", "admin" }); break; case "first.last@hotmail.com": myroles = new list<string>(new string[] { "admin" }); break; case "tyler.perry@gmail.com": myroles = new list<string>(new string[] { "client", "outsider" }); break; default: myroles = new list<string>(new string[] {"[user] not found."}); break; } homecoming myroles; } #endregion } }

can offer insight here may overlooking? total details, current catch statment outputting following:

error adding user role: userid not found. system.collections.listdictionaryinternal name: admin user email: admin@abc.com user.id: 1 u.id: 1 role: root codehit: true

when comment out explicit id = "1", admin user, user.id , u.id becomes: ab753316-3d7b-4f98-a13a-d19f7c926976. had thought might helper methods of gettestusers() or getuserroles(u.email) issue, between try/catch , codehit boolean variable using, have verified issue coming manager.addtorole(user.id, role).

i leaving here might have had similar issue. had same "symptoms". turns out problem related password beingness stored not adhering configured password policy (at to the lowest degree 1 uppercase char, 1 lowercase char, etc etc).

c# asp.net-mvc-5 entity-framework-6 asp.net-identity code-first-migrations

No comments:

Post a Comment