public async Task <User> Authenticate(AuthenticateVM model) { var app = await _applicationRepository.GetByAPIKeyAsync(model.APIKey); if (app == null) { app = await _applicationRepository.CreateAsync(new Application() { APIKey = model.APIKey, Name = model.ClientId }); } var user = await _userRepository.GetByExternalIdAsync(app.Id, model.UserExternalId); if (user == null) { user = await _userRepository.CreateAsync(new User() { FullName = model.Fullname, ExternalId = model.UserExternalId, AppId = app.Id, IsOnline = false, IsActive = true, Activities = new List <Activity> (), Connections = new List <Connection> () }); } else { user.FullName = model.Fullname; await _userRepository.updateFullNameAsync(user.Id, user.FullName); } return(user); }
public async Task <IActionResult> SignIn(AuthenticateVM authenticateVM) { UserVM userVM = new UserVM(); HttpClient client = _helperAPI.InitializeClient(); var contentType = new MediaTypeWithQualityHeaderValue("application/json"); client.DefaultRequestHeaders.Accept.Add(contentType); var content = new StringContent(JsonConvert.SerializeObject(authenticateVM), Encoding.UTF8, "application/json"); HttpResponseMessage UserVMRes = await client.PostAsync("api/users/authenticate", content); // HttpResponseMessage UserVMRes = await client.GetAsync("api/AspNetUserVMs/authenticate"); //Checking the response is successful or not which is sent using HttpClient if (UserVMRes.IsSuccessStatusCode) { //Storing the response details recieved from web api var result = UserVMRes.Content.ReadAsStringAsync().Result; //Deserializing the response recieved from web api and storing into the Employee list userVM = JsonConvert.DeserializeObject <UserVM>(result); TempData["Token"] = userVM.Token; //TempData["Name"] = userVM.FirstName + ' ' + userVM.LastName; //TempData["UserId"] = userVM.Id; HttpContext.Session.SetString("token", userVM.Token); //TempData["User"] = UserVM; HttpContext.Session.SetString("Name", userVM.FirstName + ' ' + userVM.LastName); //HttpContext.Session.SetString("Role", UserVM.RoleId.ToString()); //HttpContext.Session.SetString("token", UserVM.Token); } RoleVM roleVM = new RoleVM(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userVM.Token); HttpResponseMessage roleVMRes = await client.GetAsync("api/roles/" + userVM.RoleId); if (roleVMRes.StatusCode == HttpStatusCode.Unauthorized) { ViewBag.Message = "Unauthorized!"; } //Checking the response is successful or not which is sent using HttpClient if (roleVMRes.IsSuccessStatusCode) { //Storing the response details recieved from web api var roleResult = roleVMRes.Content.ReadAsStringAsync().Result; //Deserializing the response recieved from web api and storing into the Role list roleVM = JsonConvert.DeserializeObject <RoleVM>(roleResult); userVM.Role = roleVM; HttpContext.Session.SetString("Role", roleVM.Name); HttpContext.Session.SetString("User", JsonConvert.SerializeObject(userVM)); } TempData["User"] = JsonConvert.SerializeObject(userVM); //return View( "~/Views/Home/Index", UserVM); //returning the employee list to view //return RedirectToAction("Index", "Home", UserVM.Token ); //return RedirectToAction("Index", "Home", new { Token = userVM.Token }); return(RedirectToAction("Index", "Home")); }