public HttpResponseMessage Logout(AccessToken token) { if (token == null) return Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(false)); var tokenArray = token.Token.Split('|'); if (tokenArray.Length != 2) { return Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(false)); } LogonResponseModel logonResponse; var driverId = long.Parse(tokenArray[1]); var response = _driverRepository.ValidateToken(driverId, token.Token); switch (response.Validness) { case TokenValidness.Valid: UpdateLoggedOutDriver(driverId); logonResponse = AccountHelper.GetLogonResponseModel(true, ""); break; case TokenValidness.Expired: UpdateLoggedOutDriver(driverId); logonResponse = AccountHelper.GetLogonResponseModel(true, ""); break; case TokenValidness.Invalid: logonResponse = AccountHelper.GetLogonResponseModel(false); break; default: logonResponse = AccountHelper.GetLogonResponseModel(false); break; } var responseMessage = Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, logonResponse); var cookie = new CookieHeaderValue(SecurityHelper.AccessTokenCookieName, "") { Expires = DateTimeOffset.Now.AddDays(14), Path = "/" }; responseMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie }); return responseMessage; }
public HttpResponseMessage Login(LogonRequestModel logonModel) { if (ModelState.IsValid) { var driver = _driverRepository.ValidateDriver(logonModel.Email, logonModel.Password); if (driver == null) { return Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(false)); } else { if (driver.Token != null) { AccountHelper.SetToken(driver.Token, driver.DriverId); } else { var token = new AccessToken(); AccountHelper.SetToken(token, driver.DriverId); driver.Token = token; } _driverRepository.UpdateDriver(driver); long carId = 0; if (driver.Cars != null) { var car = driver.Cars.FirstOrDefault(c => c.IsCurrentCar); if (car != null) { carId = car.CarId; } } var responseMessage = Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(true, driver.Token.Token, driver.DriverId, driver.EmailAddress, driver.FirstName, driver.LastName, carId)); SecurityHelper.SetUseronThread(driver); var cookie = new CookieHeaderValue(SecurityHelper.AccessTokenCookieName, driver.Token.Token); cookie.Expires = DateTimeOffset.Now.AddDays(14); cookie.Path = "/"; responseMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie }); return responseMessage; } } throw new HttpResponseException(HttpStatusCode.BadRequest); }
public static void SetToken(AccessToken accessToken, long driverId) { accessToken.ExpiresOn = DateTime.UtcNow.AddDays(2); accessToken.IssuedOn = DateTime.UtcNow; accessToken.Token = string.Format("{0}|{1}", SecurityHelper.CreateToken(TokenLength), driverId); }