public async Task <UserLoginResponse> SignIn(UserSignInRequest request) { LogUtil.Debug("SignIn : " + request.Username + " In time: " + DateTime.UtcNow.ToString()); IAuthenticationManager authentication = HttpContext.Current.GetOwinContext().Authentication; UserLoginResponse response = await Task.Run(() => _userService.UserSignIn(request)); string token = string.Empty; if (response != null && response.ErrorCode == LAMPConstants.API_SUCCESS_CODE) { UserSessionToken tokenObj = _authService.CreateSessionTokenObject(response.UserId, response.Email, response.StudyId); if (tokenObj.ErrorCode == LAMPConstants.API_SUCCESS_CODE) { response.SessionToken = tokenObj.SessionToken; } else { response = new UserLoginResponse { ErrorCode = LAMPConstants.API_SESSION_TOKEN_UPDATION_FAILED, ErrorMessage = ResourceHelper.GetStringResource(LAMPConstants.API_SESSION_TOKEN_UPDATION_FAILED) }; } } LogUtil.Debug("SignIn : " + request.Username + " Out time: " + DateTime.UtcNow.ToString()); return(response); }
/// <summary> /// Create Session token object /// </summary> /// <param name="userId">UserId</param> /// <param name="email">Email</param> /// <param name="studyId">StudyId</param> /// <returns>Session token</returns> public UserSessionToken CreateSessionTokenObject(long userId, string email, string studyId) { var response = new UserSessionToken(); try { if (Helper.IsValidEmail(email) == false) { email = string.Empty; } string token = string.Empty; IAuthenticationManager authentication = HttpContext.Current.GetOwinContext().Authentication; UserAuthenticateRequest userRequest = new UserAuthenticateRequest { UserID = userId, Username = email, StudyID = studyId }; token = SignIn(userRequest, Startup.OAuthOptions, authentication); User user = _UnitOfWork.IUserRepository.GetById(userId); if (user != null) { user.SessionToken = token; _UnitOfWork.IUserRepository.Update(user); _UnitOfWork.Commit(); response = new UserSessionToken { SessionToken = token, ErrorCode = LAMPConstants.API_SUCCESS_CODE }; } } catch (Exception ex) { LogUtil.Error(ex); response = new UserSessionToken { ErrorCode = LAMPConstants.API_UNEXPECTED_ERROR, ErrorMessage = ResourceHelper.GetStringResource(LAMPConstants.API_UNEXPECTED_ERROR) }; } return(response); }
/* public string UserId() * { * // The user's ID is available in the NameIdentifier claim * var user = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); * string userId = user.GetClaims(HttpContext.Current.User.Identity.GetUserId()).FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; * * return userId; * * }*/ /* public string UserInformationToken() * { * // Retrieve the access_token claim which we saved in the OnTokenValidated event * var user = GetCurrentUserId(); * string accessToken = user.GetClaims(HttpContext.Current.User.Identity.GetUserId()).FirstOrDefault(c => c.Type == "access_token").Value; * //string accessToken = User.Claims.FirstOrDefault(c => c.Type == "access_token").Value; * * // If we have an access_token, then retrieve the user's information * if (!string.IsNullOrEmpty(accessToken)) * { * var apiClient = new AuthenticationApiClient(_configuration["auth0:domain"]); * var userInfo = await apiClient.GetUserInfoAsync(accessToken); * * return userInfo; * } * * return accessToken; * } */ /* public object Claims() * { * var user = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); * * return user.GetClaims(HttpContext.Current.User.Identity.GetUserId()).Select(c => new * { * Type = c.Type, * Value = c.Value * }); * } */ /// <summary> /// Extends the validity period of the current user's session in the database. /// This will configure the user's bearer authorization token to expire after /// certain period of time (e.g. 30 minutes, see UserSessionTokenTimeout in Web.config) /// </summary> public void CreateUserSession(string userName, string authToken) { /* var db = new GatewayPCIPINContext(); * * var users = db.AspNetUsers.ToList(); * var usersSessionsTokens = db.UserSessionTokens.ToList(); * * var users2 = uow.getRepository<AspNetUser>().GetAll().ToList(); * var usersSessionsTokens2 = uow.getRepository<UserSessionToken>().GetAll().ToList(); * * var userId = users2.First(u => u.Login == username).Id;*/ var userId = _userSessionTokenRepos.getAspNetUserIdByUserName(userName); var userSessionToken = new UserSessionToken() { OwnerAspNetUser_fk_Id = userId, AuthToken = authToken, is_LoggedIn_LoggedOut = 1 // 1 => Is Logged In <-> 0 => Is Logged Out }; userSessionToken.LoginDateTime = DateTime.Now; userSessionToken.ExpirationDateTime = DateTime.Now + TimeSpan.FromMinutes(30); _userSessionTokenRepos.AddUserSessionToken(userSessionToken); //var userId = this.Data.Users.All().First(u => u.UserName == username).Id; /* var userId = users.First(u => u.Login == username).Id; * var userSessionToken = new UserSessionToken() * { * OwnerAspNetUser_fk_Id = userId, * AuthToken = authToken, * is_LoggedIn_LoggedOut = 1 // 1 => Is Logged In <-> 0 => Is Logged Out * };*/ // usersSessionsTokens.Add(userSessionToken); // Extend the lifetime of the current user's session: current moment + fixed timeout //userSessionToken.ExpirationDateTime = DateTime.Now + new DateTime (System.Configuration.ConfigurationManager.GetSection("MSS.MSSolutions.Properties.Settings.UserSessionTokenTimeout").ToString()); //Balise MSS.MSSolutions.Properties.Settings.UserSessionTokenTimeout : added by me in web.config' => give 30 minutes de plus; //uow.getRepository<UserSessionToken>().Add(userSessionToken); //db.SaveChanges(); }
public async Task <UserResponse> GuestUserSignUp(GuestUserSignUpRequest request) { UserResponse response = await Task.Run(() => _userService.GuestUserSignUp(request)); if (response != null && response.ErrorCode == LAMPConstants.API_SUCCESS_CODE) { UserSessionToken tokenObj = _authService.CreateSessionTokenObject(response.UserId, response.Email, response.StudyId); if (tokenObj.ErrorCode == LAMPConstants.API_SUCCESS_CODE) { response.SessionToken = tokenObj.SessionToken; } else { response = new UserResponse { ErrorCode = LAMPConstants.API_SESSION_TOKEN_UPDATION_FAILED, ErrorMessage = ResourceHelper.GetStringResource(LAMPConstants.API_SESSION_TOKEN_UPDATION_FAILED) }; } } return(response); }
public void addUserSessionToken(UserSessionToken userSessionToken) { }