public async Task<IHttpActionResult> RegisterUser(UserAccountInputModel model) { if (model == null) { return this.BadRequest("Invalid user data"); } if (!ModelState.IsValid) { return this.BadRequest(this.ModelState); } var user = new User { UserName = model.Username }; var identityResult = await this.UserManager.CreateAsync(user, model.Password); if (!identityResult.Succeeded) { return this.GetErrorResult(identityResult); } // Auto login after registrаtion (successful user registration should return access_token) var loginResult = await this.LoginUser(new UserAccountInputModel() { Username = model.Username, Password = model.Password }); return loginResult; }
public async Task<IHttpActionResult> LoginUser(UserAccountInputModel model) { if (model == null) { return this.BadRequest("Invalid user data"); } // Invoke the "token" OWIN service to perform the login (POST /api/token) 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); return this.ResponseMessage(tokenServiceResponse); }