c# - Correct Position For Specific Code, Controller, Model or somewhere else -
i have started learning c# mvc 5 room inventory project working on work. using active directory authenticate users , store these details in database can assign users rooms , tenancies have items assigned them.
i used stock mvc 5 template comes visual studio 2013 have month project coming in no previous knowledge , dont have time code fancy looking html / css front end , relying on comes out of box.
i have written code once user logged on using form authentication linked ad runs check see if user exists in database, if not creates one, if does, checks see if has been set inactive , reactivates if needed.
i have placed code in accountcontroller under login httppost action. new mvc wanted check see if correct position, or if should create new class functionality, or if should in model area create database using entity framework code first. best practice?
here code , in advance help. apologize long winded post , less stellar code. dont hold criticism rather right make mistakes again , again.
[httppost] public actionresult login(loginmodel model, string returnurl) { if (!modelstate.isvalid) { return view(model); } // put method in place allow multiple domains login. references web.config providers membershipprovider domainprovider; switch (model.domain) { case "student": domainprovider = membership.providers["studentadmembershipprovider"]; break; case "staff": domainprovider = membership.providers["staffadmembershipprovider"]; break; default: throw (new exception("this domain not supported")); } // method authenticating users on ad allow system integration , add or update user in users database if (domainprovider.validateuser(model.username, model.password)) { formsauthentication.setauthcookie(model.username, model.rememberme); //code creating new user based on successfull logged in user. //define domain link too. otherwise default staff principalcontext mycontext; if (model.domain == "staff") { mycontext = new principalcontext(contexttype.domain, "staff.domain.com"); } else { mycontext = new principalcontext(contexttype.domain, "student.domain.com"); } userprincipal aduser = userprincipal.findbyidentity(mycontext, identitytype.samaccountname, model.username); //check existence of user username matches loged in user var usersearch = db.users.count(b => b.username == aduser.samaccountname); if (usersearch == 0) { // user not exist, therefore create user user user = new user() { isactive = 1, username = aduser.samaccountname, firstname = aduser.givenname, lastname = aduser.surname, extension = aduser.voicetelephonenumber, email = aduser.emailaddress, usertypeid = 3 }; db.users.add(user); db.savechanges(); } else { // user exist, has been deactivated, therefore reactivate var activateuser = db.users.firstordefault(d => d.username == aduser.samaccountname); if (activateuser.isactive == 0) { activateuser.isactive = 1; db.savechanges(); //db.users(activateuser.userid).state = entitystate.modified; } } if (url.islocalurl(returnurl) && returnurl.length > 1 && returnurl.startswith("/") && !returnurl.startswith("//") && !returnurl.startswith("/\\")) { return redirect(returnurl); } return redirecttoaction("index", "home"); } modelstate.addmodelerror(string.empty, "the user name or password provided incorrect."); return view(model); }
Comments
Post a Comment