Here's how I'm authenticating via WebAPI - it's with Forms Auth and SimpleMembershipProvider. I'm not a seasoned coder so this could be far from quality but it should get you started. Note that this just authenticates the user, not the client (api key implementation is a TODO for me). Feedback welcome.
Checking to see if the current client/user is authenticated:
public class AccountController : ApiController
{
public static DtoService _service = new DtoService();
// GET/api/isAuthenticated
[System.Web.Http.HttpGet]
public HttpResponseMessage IsAuthenticated()
{
try
{
if (User.Identity.IsAuthenticated)
return Request.CreateResponse(HttpStatusCode.OK, WebSecurity.GetUserId(User.Identity.Name));
else
return Request.CreateResponse(HttpStatusCode.OK, false);
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
Log in:
// POST /api/login
// [System.Web.Http.AllowAnonymous]
[System.Web.Http.HttpPost]
public HttpResponseMessage LogIn(LoginModel model)
{
if (!ModelState.IsValid)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
try
{
if (User.Identity.IsAuthenticated)
return Request.CreateResponse(HttpStatusCode.Conflict, "already logged in.");
if (!WebSecurity.UserExists(model.UserName))
return Request.CreateResponse(HttpStatusCode.BadRequest, "User does not exist.");
if (WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return Request.CreateResponse(HttpStatusCode.OK, "logged in successfully");
}
return Request.CreateResponse(HttpStatusCode.BadRequest, "Login Failed.");
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
Log out:
// POST /api/logout
[System.Web.Http.HttpPost]
////[ValidateAntiForgeryToken]
[Authorize]
public HttpResponseMessage LogOut()
{
try
{
if (User.Identity.IsAuthenticated)
{
WebSecurity.Logout();
return Request.CreateResponse(HttpStatusCode.OK, "logged out successfully.");
}
return Request.CreateResponse(HttpStatusCode.Conflict, "already done.");
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
Register:
// POST: /api/register
[System.Web.Http.HttpPost]
//[ValidateAntiForgeryToken]
public HttpResponseMessage Register(RegisterModel model)
{
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
try
{
if (User.Identity.IsAuthenticated)
return Request.CreateResponse(HttpStatusCode.Conflict, "User Already Registered and Logged In");
if (WebSecurity.UserExists(model.UserName))
return Request.CreateResponse(HttpStatusCode.Conflict, "User Already Registered");
else
{
// Attempt to register the user
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
WebSecurity.Login(model.UserName, model.Password);
InitiateDatabaseForNewUser(WebSecurity.GetUserId(model.UserName));
FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
return Request.CreateResponse(HttpStatusCode.Created, WebSecurity.GetUserId(model.UserName));
}
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
}