Example #1
0
        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 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);
        }