// POST: api/ForgotPasswordDetails public async Task <HttpResponseMessage> Post([FromBody] ForgotPasswordDetails value) { try { ForgotPasswordDetailsRef FgDetailsRef = new ForgotPasswordDetailsRef(); ForgotPasswordClass fgClass = new ForgotPasswordClass(); var authorization = Request.Headers.Authorization; string UserName = string.Empty; if (authorization == null || authorization.Scheme != "Bearer") { return(Request.CreateResponse <ForgotPasswordDetailsRef>(HttpStatusCode.BadRequest, FgDetailsRef)); } if (string.IsNullOrEmpty(authorization.Parameter)) { return(Request.CreateResponse <ForgotPasswordDetailsRef>(HttpStatusCode.BadRequest, FgDetailsRef)); } var token = authorization.Parameter; bool strbool = CommonUseFunctionClass.ValidateToken(token, out UserName); if (strbool == true) { FgDetailsRef = await fgClass.GetFogotPasswordDetails(value.Email, value.Password, value.ConfirmPassword, value.reCaptcha); if (FgDetailsRef != null) { return(Request.CreateResponse <ForgotPasswordDetailsRef>(HttpStatusCode.OK, FgDetailsRef)); } else { FgDetailsRef.Status = "Failure"; return(Request.CreateResponse <ForgotPasswordDetailsRef>(HttpStatusCode.BadRequest, FgDetailsRef)); } } else { FgDetailsRef.Status = "Failure"; FgDetailsRef.ErrorMessage.Add("Token Is Not valid"); return(Request.CreateResponse <ForgotPasswordDetailsRef>(HttpStatusCode.BadRequest, FgDetailsRef)); } } catch (Exception xp) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, xp.Message)); } finally { } return(null); }
// private const string Secret = "db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw=="; //public static string GenerateToken(string username, int expireMinutes = 20) //{ // var symmetricKey = Convert.FromBase64String(Secret); // var tokenHandler = new JwtSecurityTokenHandler(); // var now = DateTime.UtcNow; // var tokenDescriptor = new SecurityTokenDescriptor // { // Subject = new ClaimsIdentity(new[] // { // new Claim(ClaimTypes.Name, username) // }), // Expires = now.AddMinutes(Convert.ToInt32(expireMinutes)), // SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature) // }; // var stoken = tokenHandler.CreateToken(tokenDescriptor); // var token = tokenHandler.WriteToken(stoken); // return token; //} //public bool CheckUser(string username, int password) //{ // try // { // LoginDetailsRef loginRef = new LoginDetailsRef(); // LogInDetailsClass logincls = new LogInDetailsClass(); // loginRef = logincls.GetLogInDetailsPage(username, password); // if (loginRef.LogInData.UserName.Trim() != string.Empty || loginRef.LogInData.Password.Value != 0 || string.IsNullOrWhiteSpace(Convert.ToString(loginRef.LogInData.Password).Trim())) // { // return true; // } // else // { // return false; // } // } // catch (Exception xp) // { // } // finally // { // } // return false; //} #endregion // POST: api/LogInDetails //public void Post([FromBody]string value) //{ //} #region Token Validation Method //private static bool ValidateToken(string token, out string username) //{ // username = null; // var simplePrinciple = GetPrincipal(token); // var identity = simplePrinciple.Identity as ClaimsIdentity; // if (identity == null) // return false; // if (!identity.IsAuthenticated) // return false; // var usernameClaim = identity.FindFirst(ClaimTypes.Name); // username = usernameClaim?.Value; // if (string.IsNullOrEmpty(username)) // return false; // // More validate to check whether username exists in system // return true; //} //protected Task<IPrincipal> AuthenticateJwtToken(string token) //{ // string username; // if (ValidateToken(token, out username)) // { // // based on username to get more information from database in order to build local identity // var claims = new List<Claim> // { // new Claim(ClaimTypes.Name, username) // // Add more claims if needed: Roles, ... // }; // var identity = new ClaimsIdentity(claims, "Jwt"); // IPrincipal user = new ClaimsPrincipal(identity); // return Task.FromResult(user); // } // return Task.FromResult<IPrincipal>(null); //} //public static ClaimsPrincipal GetPrincipal(string token) //{ // try // { // var tokenHandler = new JwtSecurityTokenHandler(); // var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken; // if (jwtToken == null) // return null; // var symmetricKey = Convert.FromBase64String(Secret); // var validationParameters = new TokenValidationParameters() // { // RequireExpirationTime = true, // ValidateIssuer = false, // ValidateAudience = false, // IssuerSigningKey = new SymmetricSecurityKey(symmetricKey) // }; // SecurityToken securityToken; // var principal = tokenHandler.ValidateToken(token, validationParameters, out securityToken); // return principal; // } // catch (Exception xp) // { // //should write log // return null; // } //} #endregion // PUT: api/LogInDetails/5 public HttpResponseMessage Put(int id, [FromBody] string value) { LoginDetailsRef loginref = new LoginDetailsRef(); try { var authorization = Request.Headers.Authorization; string UserName = string.Empty; if (authorization == null || authorization.Scheme != "Bearer") { return(Request.CreateResponse <LoginDetailsRef>(HttpStatusCode.BadRequest, loginref)); } if (string.IsNullOrEmpty(authorization.Parameter)) { // context.ErrorResult = new AuthenticationFailureResult("Missing Jwt Token", request); return(Request.CreateResponse <LoginDetailsRef>(HttpStatusCode.BadRequest, loginref)); } var token = authorization.Parameter; bool strbool = CommonUseFunctionClass.ValidateToken(token, out UserName); if (strbool == true) { loginref.Status = "Success"; return(Request.CreateResponse <LoginDetailsRef>(HttpStatusCode.OK, loginref)); } else { loginref.Status = "Failure"; return(Request.CreateResponse <LoginDetailsRef>(HttpStatusCode.BadRequest, loginref)); } } catch (Exception xp) { loginref.Status = "Failure"; return(Request.CreateResponse <LoginDetailsRef>(HttpStatusCode.BadRequest, loginref)); } finally { } return(null); }