public bool CloseDoor(int user_id, int door_id) { authentication = new Authenticate(); if (authentication.IsAuthenticated(user_id, door_id)) { return(simulation.GetBuilding(0).CloseDoor(user_id, door_id)); } return(false); }
public async Task <IActionResult> Login([FromForm] LoginReq req) { var(b, token) = await _Auth.IsAuthenticated(req); if (!b) { return(NotFound("User not found or password incorrect")); } else { return(Ok(token)); } }
/// <summary> /// Handles authentication on incoming requests by checking the authorization header for a valid bearer token. /// Where no token exists or it is invalid an Unauthorized response is returned /// Where the token is valid a <see cref="AuthenticationTicket"/> is returned containing a <see cref="ClaimsPrincipal"/> with the appropriate <see cref="ClaimsIdentity"/> /// </summary> /// <returns></returns> protected async override Task <AuthenticateResult> HandleAuthenticateAsync() { try { if (!Request.Headers.TryGetValue(HeaderNames.Authorization, out var authorization)) { return(AuthenticateResult.Fail("No Authorization header found")); } if (!Request.Headers[HeaderNames.Authorization].Any(h => h.Contains(BearerAuthenticationOptions.DefaultScheme))) { return(AuthenticateResult.Fail("Authorization header should be a Bearer token")); } var token = Request.Headers[HeaderNames.Authorization] .First() .Split(' ') [1] ; var response = await _authService.IsAuthenticated(token); if (!response.IsAuthenticated) { return(AuthenticateResult.Fail("Bearer token does not grant access to the requested resource")); } var principal = new ClaimsPrincipal ( new ClaimsIdentity(new List <Claim> { new Claim("gatewayid", response.GatewayId ?? string.Empty), new Claim("privileged", response.IsPrivileged.ToString()) }, BearerAuthenticationOptions.DefaultScheme) ); return(AuthenticateResult.Success( new AuthenticationTicket(principal, BearerAuthenticationOptions.DefaultScheme) )); } catch (Exception ex) { return(AuthenticateResult.Fail("Failed to authenticate request")); } }
public ActionResult Login([FromBody] LoginRequestModel request) { LoginModelOutput _ouput = new LoginModelOutput(); try { if (ModelState.IsValid) { string token; if (_authservice.IsAuthenticated(request, out token)) { DisplayUserSecurityRepository displayUserSecurity = new DisplayUserSecurityRepository(); CheckUserRepository checkUserRepository = new CheckUserRepository(); List <UserModel> collection = new List <UserModel>((IEnumerable <UserModel>)displayUserSecurity.SearchUserWithSetting(request.Email)); string refreshToken = GenerateToken(); checkUserRepository.InsertLogToken(request.Email, refreshToken, collection[0].user_id); checkUserRepository.updateUserToken(refreshToken, collection[0].user_id); _ouput.IsSuccess = true; _ouput.Code = 200; _ouput.Message = "Success Login"; _ouput.Data = collection.ToList(); _ouput.token = token; _ouput.RefreshToken = refreshToken; } else { _ouput.IsSuccess = false; _ouput.Code = 422; _ouput.Message = "Username And Password Is Not Match"; } } else { string errordetails = ""; var errors = new List <string>(); foreach (var state in ModelState) { foreach (var error in state.Value.Errors) { string p = error.ErrorMessage; errordetails = errordetails + error.ErrorMessage; } } Dictionary <string, object> dict = new Dictionary <string, object>(); dict.Add("error", errordetails); _ouput.IsSuccess = false; _ouput.Message = "error login"; _ouput.Code = 422; _ouput.CustomField = dict; } } catch (Exception ex) { _ouput.IsSuccess = false; _ouput.Code = 422; _ouput.Message = ex.Message.ToString(); } return(Ok(_ouput)); }