public async Task <IHttpActionResult> LoginUser(LoginUserBindingModel model) { if (this.User.Identity.GetUserId() != null) { return(this.BadRequest("User is already logged in.")); } if (model == null) { return(this.BadRequest("Invalid user data")); } // Invoke the "token" OWIN service to perform the login (POST /api/token) // Use Microsoft.Owin.Testing.TestServer to perform in-memory HTTP POST request var testServer = TestServer.Create <Startup>(); var requestParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", model.Username), new KeyValuePair <string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( Startup.TokenEndpointPath, requestParamsFormUrlEncoded); if (tokenServiceResponse.StatusCode == HttpStatusCode.OK) { // Sucessful login --> create user session in the database var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var jsSerializer = new JavaScriptSerializer(); var responseData = jsSerializer.Deserialize <Dictionary <string, string> >(responseString); var authToken = responseData["access_token"]; var username = responseData["userName"]; var owinContext = this.Request.GetOwinContext(); var userSessionManager = new UserSessionManager(owinContext); userSessionManager.CreateUserSession(username, authToken); // Cleanup: delete expired sessions from the database userSessionManager.DeleteExpiredSessions(); } return(this.ResponseMessage(tokenServiceResponse)); }
public async Task <IHttpActionResult> Login(LoginUserBindingModel model) { if (model == null) { return(this.Content(HttpStatusCode.Conflict, "Model cannot be null.")); } // Invoke the "token" OWIN service to perform the login (POST /api/token) // Use Microsoft.Owin.Testing.TestServer to perform in-memory HTTP POST request //var testServer = TestServer.Create<Startup>(); var requestParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", model.Username), new KeyValuePair <string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); HttpClient httpClient = new HttpClient(); var request = HttpContext.Current.Request; var tokenServiceResponse = await httpClient. PostAsync(string.Format("{0}://{1}", request.Url.Scheme, request.Url.Authority) + Startup.OAuthOptions.TokenEndpointPath.ToString(), requestParamsFormUrlEncoded); //var tokenServiceResponse = await testServer.HttpClient.PostAsync( // Startup.TokenEndpointPath, requestParamsFormUrlEncoded); if (tokenServiceResponse.StatusCode == HttpStatusCode.OK) { // Sucessful login --> create user session in the database var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var jsSerializer = new JavaScriptSerializer(); var responseData = jsSerializer.Deserialize <Dictionary <string, string> >(responseString); var authToken = responseData["access_token"]; var username = responseData["username"]; var userSessionManager = new UserSessionManager(); userSessionManager.CreateUserSession(username, authToken); // Cleanup: delete expired sessions from the database userSessionManager.DeleteExpiredSessions(); } return(this.ResponseMessage(tokenServiceResponse)); }
public async Task <IHttpActionResult> LoginUser(LoginUserBindingModel bindingModel) { if (this.UserIdProvider.GetUserId() != null) { return(this.BadRequest("User is already logged in.")); } if (!this.ModelState.IsValid || bindingModel == null) { return(this.BadRequest("Invalid user data")); } var requestParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", bindingModel.Username), new KeyValuePair <string, string>("password", bindingModel.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var testServer = TestServer.Create <Startup>(); var tokenServiceResponse = await testServer.HttpClient.PostAsync("/api/Token", requestParamsFormUrlEncoded); if (tokenServiceResponse.StatusCode == HttpStatusCode.OK) { var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var jsSerializer = new JavaScriptSerializer(); var responseData = jsSerializer.Deserialize <Dictionary <string, string> >(responseString); var authenticationToken = responseData["access_token"]; var username = responseData["userName"]; var owinContext = this.Request.GetOwinContext(); var userSessionManager = new UserSessionManager(owinContext); userSessionManager.CreateUserSession(username, authenticationToken); userSessionManager.DeleteExpiredSession(); } return(this.ResponseMessage(tokenServiceResponse)); }
public async Task<IHttpActionResult> LoginUser(LoginUserBindingModel model) { if (this.User.Identity.GetUserId() != null) { return this.BadRequest(UserAlreadyLoggedInMessage); } if (model == null) { return this.BadRequest(InvalidUserDataMessage); } var testServer = TestServer.Create<Startup>(); var requestParams = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", model.Username), new KeyValuePair<string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( Startup.TokenEndpointPath, requestParamsFormUrlEncoded); if (tokenServiceResponse.StatusCode == HttpStatusCode.OK) { var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var jsSerializer = new JavaScriptSerializer(); var responseData = jsSerializer.Deserialize<Dictionary<string, string>>(responseString); var authToken = responseData["access_token"]; var username = responseData["userName"]; var owinContext = this.Request.GetOwinContext(); var userSessionManager = new UserSessionManager(owinContext); userSessionManager.CreateUserSession(username, authToken); userSessionManager.DeleteExpiredSessions(); } return this.ResponseMessage(tokenServiceResponse); }
public async Task<IHttpActionResult> LoginUser(LoginUserBindingModel model) { if (model == null) { return this.BadRequest("Invalid user data"); } // Invoke the "token" OWIN service to perform the login (POST /api/token) // Use Microsoft.Owin.Testing.TestServer to perform in-memory HTTP POST request var testServer = TestServer.Create<Startup>(); var requestParams = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", model.Username), new KeyValuePair<string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( Startup.TokenEndpointPath, requestParamsFormUrlEncoded); if (tokenServiceResponse.StatusCode == HttpStatusCode.OK) { // Sucessful login --> create user session in the database var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var jsSerializer = new JavaScriptSerializer(); var responseData = jsSerializer.Deserialize<Dictionary<string, string>>(responseString); var authToken = responseData["access_token"]; var username = responseData["userName"]; var owinContext = this.Request.GetOwinContext(); var userSessionManager = new UserSessionManager(owinContext); userSessionManager.CreateUserSession(username, authToken); // Cleanup: delete expired sessions from the database userSessionManager.DeleteExpiredSessions(); } return this.ResponseMessage(tokenServiceResponse); }
public async Task<IHttpActionResult> Login(LoginBindingModel model) { if (this.User.Identity.GetUserId() != null) { return this.BadRequest("User is already logged in."); } if (model == null) { return this.BadRequest("Invalid user data."); } var testServer = TestServer.Create<Startup>(); var requestParams = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", model.Username), new KeyValuePair<string, string>("password", model.Password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( "/api/token", requestParamsFormUrlEncoded); if (tokenServiceResponse.StatusCode == HttpStatusCode.OK) { // Sucessful login --> create user session in the database var responseString = await tokenServiceResponse.Content.ReadAsStringAsync(); var jsSerializer = new JavaScriptSerializer(); var responseData = jsSerializer.Deserialize<Dictionary<string, string>>(responseString); var authToken = responseData["access_token"]; var username = responseData["userName"]; var owinContext = this.Request.GetOwinContext(); var userSessionManager = new UserSessionManager(owinContext); userSessionManager.CreateUserSession(username, authToken); // Cleanup: delete expired sessions from the database userSessionManager.DeleteExpiredSessions(); } return this.ResponseMessage(tokenServiceResponse); }