Background: I've a Web App that offers a service to my customers.
Motivation: Now I want to expose that service with the help of an API (WCF & Web API). The consumers of the service will need to authenticate.
The Problem: Most of the consumers of the API will be from my customers of the Web App.
I don't want that one client will have 2 passwords, one for the Web App and one for the API.
How can I share the Web App (MVC5) DB with other projects? like WCF for example.
I need in my WCF two methods that will run exactly like the Web App:
- Register.
- Login.
This methods are implement in my project as 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 we got this far, something 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);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to 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);
}
}