/// <summary> /// 登陆/注册 /// </summary> /// <param name="uid"></param> /// <param name="pwd"></param> /// <returns>用户对象</returns> public UserInfo LoginIn(string uid, string pwd) { //用户密码加密保存 pwd = EncryptAndDecrypt.Encrypt(pwd); var dal = new UserDal(); var tryuser = dal.ExistUser(uid); if (tryuser == null) { //若尝试登录的用户ID不存在,则直接新增该用户 dal.AddUser(new UserInfo { UserID = uid, Pwd = pwd }); } var user = new UserDal().GetUser(uid, pwd); if (user != null) { SetLoginCookie(user); } return(user); }
public LoginInfoModel GetLoginInfo() { var model = new LoginInfoModel(); var loginCookie = CookiesHelper.GetCookie(WebConfigOperation.CookieName);//是否已存在登录的用户cookie if (loginCookie != null) { //2.获取用户信息 model.UserInfo = new LoginBLL().GetUserInfo(loginCookie.Value); if (model.UserInfo == null) { return(model); } } var ykCookie = CookiesHelper.GetCookie(WebConfigOperation.YkCookieName); if (ykCookie == null) { var yk = EncryptAndDecrypt.Encrypt(DateTime.Now.ToString()); CookiesHelper.AddCookie(WebConfigOperation.YkCookieName, yk); CookiesHelper.SetCookie(WebConfigOperation.YkCookieName, DateTime.Now.AddMonths(1)); model.ykCookie = yk; } else { model.ykCookie = ykCookie.Value.ToString(); } return(model); }
/// <summary> /// 设置登陆cookie /// </summary> /// <param name="user"></param> public void SetLoginCookie(UserInfo user) { var userInfo = JsonConvert.SerializeObject(new { user.UserID, user.CreateTime }); var cookieStr = EncryptAndDecrypt.Encrypt(userInfo); CookiesHelper.AddCookie(WebConfigOperation.CookieName, cookieStr); CookiesHelper.SetCookie(WebConfigOperation.CookieName, DateTime.Now.AddMonths(1)); }
public async Task SaveToFileAsync(IStorageFile storageFile, string password, PasswordKeeper passwordKeeper) { using (var stream = await storageFile.OpenTransactedWriteAsync()) { using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(passwordKeeper.SaveToJson()))) { EncryptAndDecrypt.Encrypt(mem, password, stream.Stream.AsStreamForWrite()); } await stream.CommitAsync(); } }
/// <summary> /// 保存游戏数据 /// </summary> /// <param name="game"></param> public void SetGameData(string key, GameInfo game) { //IIS缓存 CacheExts <GameInfo> .SetValue(key, game, noSlidingExpiration : false); //客户端持久化存储 var json = JsonConvert.SerializeObject(game); var cookieStr = EncryptAndDecrypt.Encrypt(json); CookiesHelper.AddCookie(key, cookieStr); CookiesHelper.SetCookie(key, DateTime.Now.AddMonths(1)); }
public async Task <UserDto> ValidateUsersServices(string email, string password) { try { string encrypted = string.Empty; string encryptkey = _appSettings.EncryptAndDecryptKey; if (!string.IsNullOrEmpty(password)) { encrypted = EncryptAndDecrypt.Encrypt(password, encryptkey); password = encrypted; } User userModel = await _iUsersRepository.ValidateUsersRepository(email, password); UserDto userDto = _mapper.Map <UserDto>(userModel); if (userModel.Role != null && !(string.IsNullOrEmpty(userModel.Role.Role1))) { userDto.Role = userModel.Role.Role1; } // return null if user not found if (userDto == null) { return(null); } // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, Convert.ToString(userDto.UserId)), new Claim(ClaimTypes.Role, userDto.Role) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); userDto.Token = tokenHandler.WriteToken(token); // remove password before returning userDto.Password = null; return(userDto); } catch (Exception) { throw; } }
//public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) //{ // var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>(); // ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); // if (user == null) // { // context.SetError("invalid_grant", "The user name or password is incorrect."); // return; // } // ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, // OAuthDefaults.AuthenticationType); // ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, // CookieAuthenticationDefaults.AuthenticationType); // AuthenticationProperties properties = CreateProperties(user.UserName); // AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); // context.Validated(ticket); // context.Request.Context.Authentication.SignIn(cookiesIdentity); //} public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { try { _login = new MSEADEW_BAL.BAL.Admin.Login(); _dataSet = new DataSet(); _dataSet = _login.GetLoginDetails(context.UserName); string encriptedPassword = EncryptAndDecrypt.Encrypt(context.Password); if (_dataSet.Tables[0].Rows[0][2].ToString() == encriptedPassword) { var Identity = new ClaimsIdentity(context.Options.AuthenticationType); Identity.AddClaim(new Claim("UserId", context.UserName)); var props = new AuthenticationProperties(new Dictionary <string, string> { { "Email", _dataSet.Tables[0].Rows[0][1].ToString() }, { "RoleId", _dataSet.Tables[0].Rows[0][5].ToString() }, { "FirstName", _dataSet.Tables[0].Rows[0][3].ToString() }, { "LastName", _dataSet.Tables[0].Rows[0][4].ToString() }, { "UserId", _dataSet.Tables[0].Rows[0][0].ToString() }, }); AuthenticationTicket ticket = new AuthenticationTicket(Identity, props); context.Validated(Identity); context.Validated(ticket); } else { context.SetError("invalid_grant", "The user name or password is incorrect."); //if (user.Invalid_Attempts_Count < 5 && userInvalidAttempts.IsLocked == false) } } catch (Exception exception) { throw exception; } }
public async Task <int> InsertUserServices(UserDto userDto) { int result = 0; try { string encrypted = string.Empty; string key = _appSettings.EncryptAndDecryptKey; if (!string.IsNullOrEmpty(userDto.Password)) { encrypted = EncryptAndDecrypt.Encrypt(userDto.Password, key); userDto.Password = encrypted; } User user = _mapper.Map <User>(userDto); result = await _iUsersRepository.InsertUserRepository(user); } catch (Exception) { throw; } return(result); }
public static async Task <string> GetTinyUrl(GeoCoordinate gc) { //https://<domain>/default.aspx?V=2&t=SKe/CBsBqQ3mMrXFT4s2UIk6ADFMpCqYljOpw00KNmkvVt8LnTxgiozDf9qIYF0MMSGsfzGnb9P8SEgPKmqo9w==&ut=635651427039540000&d=635651229039990000&l=17.4315747&g=78.3433867 string url = Config.GuardianPortalUrl + @"default.aspx?V=2&"; if (Globals.IsRegisteredUser && !Globals.IsDataNetworkAvailable) { string encryptedParameters = EncryptAndDecrypt.Encrypt(string.Format("p={0}&s={1}&f={2}", Globals.User.CurrentProfileId, Globals.CurrentProfile.SessionToken, Globals.CurrentProfile.IsSOSOn)); string encodeEncryptedParams = EncryptAndDecrypt.EncodeString(encryptedParameters); url += string.Format("t={0}&ut={1}&", encodeEncryptedParams, DateTime.UtcNow.Ticks.ToString()); } url += string.Format("d={0}&l={1}&g={2}", DateTime.Now.Ticks.ToString(), gc.Latitude, gc.Longitude); if (Globals.IsRegisteredUser && Globals.IsDataNetworkAvailable) { url = string.Format("pr={0}&s={1}", Globals.User.CurrentProfileId, Globals.CurrentProfile.SessionToken); } return(await Utility.GetShortUrl(url)); }
protected void btnEncrypt_Click(object sender, EventArgs e) { txtToDecrypt.Text = EncryptAndDecrypt.Encrypt(txtToEncrypt.Text, true); }