Background: I have a web application offering services to my clients.
Motivation: Now I want to expose this service using the API (WCF and Web API). Consumers of the service will need authentication.
Problem: Most consumers of the API will come from my web application clients.
I do not want one client to have 2 passwords, one for the web application and one for the API.
How can I share a web application database (MVC5) with other projects? e.g. WCF.
I need two methods in my WCF that will work just like a web application:
These methods are implemented in my project as follows:
Registration:
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);
}
return View(model);
}
:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid || User.Identity.IsAuthenticated)
{
return View(model);
}
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);
}
}