public override void OnException(ExceptionContext context) { StringValues authorizationToken; if (context.Exception != null) { var encodedString = context.HttpContext.Request.Headers.TryGetValue("Token", out authorizationToken); if (!string.IsNullOrEmpty(authorizationToken.First())) { var key = EncryptionLibrary.DecryptText(authorizationToken.First()); string[] parts = key.Split(new char[] { ':' }); var customerId = Convert.ToInt32(parts[0]); var exception = context.Exception; var model = new LogModel { ShortMessage = exception.Message, FullMessage = exception?.ToString() ?? string.Empty, CustomerId = customerId, IpAddress = context.HttpContext.Connection.RemoteIpAddress?.ToString(), PageUrl = $"{context.HttpContext.Request.PathBase}{context.HttpContext.Request.Path}{context.HttpContext.Request.QueryString}", CreatedOn = DateTime.UtcNow, PageReferrer = $"{context.HttpContext.Request.Headers[HeaderNames.Referer]}", }; _logService.InsertLog(model); } } }
private bool Authorize(HttpActionContext actionContext) { try { var encodedString = actionContext.Request.Headers.GetValues("WeSchoolAuthorization").First(); bool validFlag = false; if (!string.IsNullOrEmpty(encodedString)) { var key = EncryptionLibrary.DecryptText(encodedString); string[] parts = key.Split(new char[] { ':' }); var UserID = Convert.ToInt32(parts[0]); // Studentcode var RandomKey = parts[1]; // Random Key var CompanyID = parts[2]; // Admission No long ticks = long.Parse(parts[3]); // Ticks DateTime IssuedOn = new DateTime(ticks); // Issued On var ClientID = parts[4]; // Admission No // By passing this parameter var registerModel = (from register in db.Applicationsdlps where register.AdmissionNo == CompanyID && register.Code == UserID && register.AdmissionNo == ClientID select register).FirstOrDefault(); if (registerModel != null) { // Validating Time var ExpiresOn = (from token in db.TokensManagers where token.StudentCode == UserID select token.ExpiresOn).FirstOrDefault(); // Validating Token var TokenKey = (from token in db.TokensManagers where token.StudentCode == UserID select token.TokenKey).FirstOrDefault(); if ((encodedString != TokenKey)) { validFlag = false; } else { if ((DateTime.Now > ExpiresOn)) { validFlag = false; } else { validFlag = true; } } } else { validFlag = false; } } return(validFlag); } catch (Exception) { return(false); } }
public static string GetUserId() { HttpContextAccessor dd = new HttpContextAccessor(); var Headers = dd.HttpContext.Request.Headers; string Token = string.Empty; foreach (var key in Headers.Keys) { if (key.ToLower() == "token") { Token = Headers[key]; } } if (string.IsNullOrEmpty(Token)) { Token = "Can't Get Account Token"; } else { var Key = EncryptionLibrary.DecryptText(Token); string[] Parts = Key.Split(new string[] { ":~!@#" }, StringSplitOptions.None); Token = Parts[0]; } return(Token); }
public async Task <UsersProfileViewModel> GetAllTransactions() { var userId = Convert.ToInt32(this.User.FindFirstValue(ClaimTypes.Name)); var userProfile = await _userProfile.GetUserByIdAsync(userId); var tempUserProfile = mapper.Map <UsersProfileViewModel>(userProfile); tempUserProfile.Password = EncryptionLibrary.DecryptText(tempUserProfile.Password); return(tempUserProfile); }
private bool Authorize(HttpActionContext actionContext) { try { var encodedString = actionContext.Request.Headers.GetValues("Token").First(); bool validFlag = false; if (!string.IsNullOrEmpty(encodedString)) { var key = EncryptionLibrary.DecryptText(encodedString); string[] parts = key.Split(new char[] { ':' }); var RandomKey = parts[0]; // Random Key var UserID = Convert.ToInt32(parts[1]); // UserID var RoleID = Convert.ToInt32(parts[2]); // RoleID var SchoolID = Convert.ToInt32(parts[3]); // SchoolID var AcademicYearID = Convert.ToInt32(parts[4]); // AcademicYearID long ticks = long.Parse(parts[5]); // Ticks DateTime IssuedOn = new DateTime(ticks); var tokenModel = (from token in db.Tokens where token.TokenKey == RandomKey select token).FirstOrDefault(); if (tokenModel != null) { var ExpiresOn = (from token in db.Tokens where token.TokenID == tokenModel.TokenID select token.ExpiresOn ).FirstOrDefault(); if ((DateTime.Now > ExpiresOn)) { validFlag = false; } else { validFlag = true; } } else { validFlag = false; } } return(validFlag); } catch (Exception) { return(false); } }
public static apiheaderdata GetAuthorize(string encodedString) { apiheaderdata apidata = new apiheaderdata(); if (!string.IsNullOrEmpty(encodedString)) { var key = EncryptionLibrary.DecryptText(encodedString); string[] parts = key.Split(new char[] { ':' }); apidata.UserID = Convert.ToInt32(parts[1]); // UserID apidata.RoleID = Convert.ToInt32(parts[2]); // RoleID apidata.SchoolID = Convert.ToInt32(parts[3]); // SchoolID apidata.AcademicYearID = Convert.ToInt32(parts[4]); // AcademicYearID } return(apidata); }
public override void OnActionExecuting(ActionExecutingContext context) { StringValues authorizationToken; try { var encodedString = context.HttpContext.Request.Headers.TryGetValue("Token", out authorizationToken); if (!string.IsNullOrEmpty(authorizationToken.First())) { var key = EncryptionLibrary.DecryptText(authorizationToken.First()); string[] parts = key.Split(new char[] { ':' }); var customerId = Convert.ToInt32(parts[0]); // customerId var RandomKey = parts[1]; // Random Key var customerRoleIds = parts[2].Split(',').Select(int.Parse).ToList(); // UserTypeID long ticks = long.Parse(parts[3]); // Ticks DateTime IssuedOn = new DateTime(ticks); var customer = _customerService.ValidateCustomerRole(customerId, (int)CustomerRoleEnum.Admin); if (customer == null) { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Result = new UnauthorizedResult(); } return; } else { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Result = new UnauthorizedResult(); } } catch (Exception) { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Result = new UnauthorizedResult(); } base.OnActionExecuting(context); }
private bool Authorize(HttpActionContext actionContext) { try { var encodeString = actionContext.Request.Headers.GetValues("Token").First(); bool validFlag = false; if (!string.IsNullOrEmpty(encodeString)) { var key = EncryptionLibrary.DecryptText(encodeString); string[] parameters = key.Split(new char[] { ':' }); var UserId = Convert.ToInt32(parameters[0]); var RandomKey = parameters[1]; var Deviceid = parameters[2]; long ticks = long.Parse(parameters[3]); DateTime issuedOn = new DateTime(ticks); var ClientId = parameters[4]; var registerModel = (from register in _context.Key where register.DeviceId == Deviceid && register.UserId == UserId && register.ClientId == ClientId select register).FirstAsync().Result; if (registerModel != null) { var ExpiersOn = (from token in _context.Token where token.DeviceId == Deviceid select token.ExpiersOn).FirstAsync().Result; validFlag = DateTime.Now <= ExpiersOn; } else { validFlag = false; } } return(validFlag); } catch (Exception e) { //Console.WriteLine(e); throw; } }
public string PostByHeader([FromBody] dynamic jsonData) { var headers = Request.Headers; if (headers.Contains("Authorization")) { string token = headers.GetValues("Authorization").First(); if (!repository.isValidToken(token)) { return("Invalid token"); } else { string username = EncryptionLibrary.DecryptText(token); return(username); } } return(""); //// GET: api/ProductDetails //[HttpGet] //public IEnumerable<Asset> GetProducts() //{ // try // { // var assets = db.Asset.ToList(); // return assets; // } // catch (System.Exception) // { // throw; // } //} }
public bool Comparepassword(ChangePasswordModel objpassword) { try { var encryptedPassword = (from user in _DatabaseContext.UserMasterTB where user.Username == objpassword.Username select user.Password).SingleOrDefault(); string OldDecryptpassword = objpassword.OldPassword; if (String.Equals(EncryptionLibrary.DecryptText(encryptedPassword), OldDecryptpassword)) { return(true); } else { return(false); } } catch (Exception) { throw; } }
///<summary> ///This method is tagged to the Employee ID textbox textchanged event; when a valid employee is entered, employee data is retrieved from the database and populates the fields. ///</summary> /// <returns></returns> /// private void TxtEmpId_TextChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtEmpId.Text) && !string.IsNullOrWhiteSpace(txtEmpId.Text)) { var employeeDetails = GetEmployeeDetails(txtEmpId.Text.Trim()); if (employeeDetails != null) { txtEmpName.Text = employeeDetails.FirstName + " " + employeeDetails.LastName; var user = GetUserDetails(txtEmpId.Text.Trim()); if (user != null) { txtUsername.Text = user.UserName; txtPassword.Text = EncryptionLibrary.DecryptText(user.UserPassword, user.UserName); txtConfirmPassword.Text = EncryptionLibrary.DecryptText(user.UserPassword, user.UserName); chkBxChangePassword.Checked = user.MustChangePassword; chkBxIsAdmin.Checked = user.IsAdmin; chkBxIsDisabled.Checked = user.IsDisabled; lblSystemMessage.Text = string.Empty; btnAdd.Enabled = false; btnEdit.Enabled = true; btnDelete.Enabled = true; } else { btnAdd.Enabled = true; btnEdit.Enabled = false; btnDelete.Enabled = false; } } } }
public LoginResponse Post([FromBody] UserDetails UserDetail) { try { LoginResponse loginresponse = new LoginResponse(); if (string.IsNullOrEmpty(UserDetail.Username) || string.IsNullOrEmpty(UserDetail.Password)) { loginresponse.Username = string.Empty; loginresponse.Token = string.Empty; loginresponse.UserTypeID = 0; return(loginresponse); } var encryptedPassword = (from user in _DatabaseContext.UserDetails where user.Username == UserDetail.Username select user.Password).SingleOrDefault(); if (!string.IsNullOrEmpty(encryptedPassword)) { if (EncryptionLibrary.DecryptText(encryptedPassword) == UserDetail.Password) { string Encryptpassword = EncryptionLibrary.EncryptText(UserDetail.Password); var isUserExists = (from user in _DatabaseContext.UserDetails where user.Username == UserDetail.Username && user.Password == Encryptpassword select user).Count(); if (isUserExists > 0) { var userDetails = (from user in _DatabaseContext.UserDetails join usertype in _DatabaseContext.UserType on user.UserTypeID equals usertype.UserTypeID where user.Username == UserDetail.Username && user.Password == Encryptpassword select new UserDetailViewModel { UserId = user.UserId, UserTypeID = user.UserTypeID, UserTypeName = usertype.UserTypeName, Username = user.Username }).SingleOrDefault(); if (userDetails != null) { GenerateToken(userDetails, loginresponse); } return(loginresponse); } else { loginresponse.Username = string.Empty; loginresponse.Token = string.Empty; loginresponse.UserTypeID = 0; return(loginresponse); } } } loginresponse.Username = string.Empty; loginresponse.Token = string.Empty; loginresponse.UserTypeID = 0; return(loginresponse); } catch (Exception) { throw; } }
public override void OnActionExecuting(ActionExecutingContext context) { StringValues authorizationToken; try { var encodedString = context.HttpContext.Request.Headers.TryGetValue("Token", out authorizationToken); if (!string.IsNullOrEmpty(authorizationToken.First())) { var key = EncryptionLibrary.DecryptText(authorizationToken.First()); string[] parts = key.Split(new char[] { ':' }); var UserID = Convert.ToInt32(parts[0]); // UserID var RandomKey = parts[1]; // Random Key var UserTypeID = Convert.ToInt32(parts[2]); // UserTypeID long ticks = long.Parse(parts[3]); // Ticks DateTime IssuedOn = new DateTime(ticks); if (UserTypeID == 1 || UserTypeID == 2) { var registerModel = (from register in _databasecontext.TokenManager where register.UserID == UserID && register.UserID == UserID select register).FirstOrDefault(); if (registerModel != null) { // Validating Time var ExpiresOn = (from token in _databasecontext.TokenManager where token.UserID == UserID select token.ExpiresOn).FirstOrDefault(); if ((DateTime.Now > ExpiresOn)) { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Result = new JsonResult("Unauthorized"); } else { } } else { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Result = new JsonResult("Unauthorized"); } } else { context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; context.Result = new JsonResult("Unauthorized"); } } } catch (Exception) { throw; } base.OnActionExecuting(context); }
private bool isAuthorize(string Token, string IpAddress) { bool _result = true; try { var encodeString = Token;//values.First();//req.Request.Headers.GetValues("Token").First(); if (string.IsNullOrEmpty(encodeString)) { _Message = "Invalid Token"; return(false); } var Key = EncryptionLibrary.DecryptText(encodeString); string[] Parts = Key.Split(new string[] { ":~!@#" }, StringSplitOptions.None); if (Parts.Count() < 6) { _Message = "Invalid Token"; return(false); } var UserName = Parts[0]; var RandomKey = Parts[1]; var IdMachine = Parts[2]; long tik = long.Parse(Parts[3]); DateTime ExpireOn = new DateTime(tik); var Psswd = Parts[4]; var Ip = Parts[5]; if (Ip != IpAddress) { _Message = "Invalid Ip Address"; return(false); } var Auth = new AuthLogin(); Auth.Account = UserName; Auth.Psswd = Psswd; var don = this.getDataAuth(Auth); if (don.Count == 0) { _Message = "Invalid User"; return(false); } else { UserLogs uslog = _userlogsRepo.GetDataBy(Auth.Account, Token); if (DateTime.Now > uslog.ExpireOn) { _Message = "Your Session Has Expired"; return(false); } } } catch (Exception ex) { _result = false; _Message = ex.Message; //throw (ex); } return(_result); }