public async Task <ActionResult <UserDto> > GetCurrentUser() { var user = await _userManager.FindByEmailClaimAsync(HttpContext.User); return(new UserDto { DisplayName = user.DisplayName, Email = user.Email, Token = _token.CreateToken(user) }); }
public async Task <ActionResult <ReturnUserDTO> > GetCurrentUser() { var user = await _userManager.FindByEmailFromClaimsPrinciple(HttpContext.User); if (user == null) { return(Unauthorized(new ErrorRes(401))); } return(new ReturnUserDTO { Email = user.Email, Token = _tokenService.CreateToken(user), DisplayName = user.DisplayName }); }
public async Task <IActionResult> CreateToken([FromBody] LoginDto model) { if (ModelState.IsValid) { var user = await _userService.FindByNameAsync(model.Username); if (user != null) { var result = await _userService.SignInAsyncByPassword(user, model.Password, false); if (result) { var results = _token.CreateToken(user.UserName, user.Email); return(Ok(results)); } } } return(BadRequest("Please check login values")); }
public string SignIn(User user) { int userId = userSignIn.SignIn(user); string jwt = null; if (userId > 0) { jwt = tokenHandler.CreateToken(userId, user.Username); } if (!string.IsNullOrEmpty(jwt)) { return(jwt); } throw new Exception(); }
public ActionResult AddStudentsToTest(AddStudentsToTestModel model) { if (!ModelState.IsValid) { return(View(model)); } ModelTest test = _testStore.GetTest(model.TestID); Stream inStream = model.file.InputStream; StreamReader sr = new StreamReader(inStream); try { List <string> usernames = new List <string>(); while (!sr.EndOfStream) { ModelUser user = new ModelUser(); string str = sr.ReadLine(); string[] userDetails = str.Split(','); user.Username = userDetails[0]; user.PasswordHash = userDetails[1]; usernames.Add(userDetails[0]); if (_userStore.GetUser(user.Username) == null) { _userStore.CreateUser(user, "student"); } } sr.Close(); ModelToken token = new ModelToken(); token.CreatedTime = DateTime.Now; DateTime date = test.Date; DateTime time = test.EndTime; time.AddHours(1); date.AddHours(time.Hour); date.AddMinutes(time.Minute); token.ExpiredTime = date; _tokenStore.CreateToken(token, usernames.ToArray(), model.TestID); return(RedirectToAction("ViewTests", "Admin")); } catch (Exception e) { sr.Close(); return(RedirectToAction("ViewTests", "Admin")); } }