c# - Connect to an Existing MVC 5 DB (Identity model) From External Project -


background: i've web app offers service customers.

motivation: want expose service of api (wcf & web api). consumers of service need authenticate.

the problem: of consumers of api customers of web app.

i don't want 1 client have 2 passwords, 1 web app , 1 api.

how can share web app (mvc5) db other projects? wcf example.

i need in wcf 2 methods run web app:

  • register.
  • login.

this methods implement in project follow:

register:

 public async task<actionresult> register(registerviewmodel model)     {         if (modelstate.isvalid)         {             var user = new applicationuser { username = model.username, email = model.email, organizationid = "10", datejoin = datetime.now, lockoutenddateutc=datetime.utcnow.addyears(5),lockoutenabled=false};             var result = await usermanager.createasync(user, model.password);             if (result.succeeded)             {                  identityresult resultclaim = await usermanager.addclaimasync(user.id, new claim("orgid", "10"));                  if(resultclaim.succeeded)                 {                     usermanager.addtorole(user.id, "guest");                     await signinmanager.signinasync(user, ispersistent: false, rememberbrowser: false);                     return redirecttoaction("index", "home");                 }               }             adderrors(result);         }          // if got far, failed, redisplay form         return view(model);     } 

login:

  public async task<actionresult> login(loginviewmodel model, string returnurl)     {          if (!modelstate.isvalid || user.identity.isauthenticated)         {             return view(model);         }          // doesn't count login failures towards account lockout         // enable password failures trigger account lockout, change shouldlockout: true         var result = await signinmanager.passwordsigninasync(model.username, model.password, model.rememberme, shouldlockout: false);         switch (result)         {            case signinstatus.success:                 session["timezone"] = model.offset;                     return redirecttolocal(returnurl);              case signinstatus.lockedout:                 return view("lockout");              case signinstatus.requiresverification:                 return redirecttoaction("sendcode", new { returnurl = returnurl, rememberme = model.rememberme });              case signinstatus.failure:             default:                 modelstate.addmodelerror("", "invalid login attempt.");                 return view(model);         }     } 

you can create seperate identity database , have users authenticate against that. when create usermanager/rolemanager etc can point connection string identity database


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -