public ActionResult Login(LoginUserVM model) { if (!ModelState.IsValid) { return(View(model)); } // check if the user is valid bool isValid = false; using (Db db = new Db()) { AccountFeature accountFeature = new AccountFeature(); var hashPassword = accountFeature.HashPassword(model.Password); if (db.Users.Any(x => x.Account.Equals(model.Account) && x.Password.Equals(hashPassword))) { isValid = true; } if (!isValid) { ModelState.AddModelError("", "Invalid account or password!!!"); return(View(model)); } else { FormsAuthentication.SetAuthCookie(model.Account, model.RememberMe); // see System.Web.Security return(Redirect(FormsAuthentication.GetRedirectUrl(model.Account, model.RememberMe))); } } }
public ActionResult CreateAccount(UserVM model) { // check model state if (!ModelState.IsValid) { return(View("CreateAccount", model)); } // check password match confirm password if (!model.Password.Equals(model.ConfirmPassword)) { ModelState.AddModelError("", "密碼不符"); return(View("CreateAccount", model)); } using (Db db = new Db()) { AccountFeature accountFeature = new AccountFeature(); var hashPassword = accountFeature.HashPassword(model.Password); // make sure account is unique if (db.Users.Any(x => x.Account.Equals(model.Account))) { ModelState.AddModelError("", "Account " + model.Account + "已有人使用!!"); model.Account = ""; return(View("CreateAccount", model)); } // create new userDTO UserDTO userDTO = new UserDTO() { Name = model.Name, Email = model.Email, Account = model.Account, Password = hashPassword }; // add DTO to db and save db.Users.Add(userDTO); db.SaveChanges(); // add to userroleDTO int userId = userDTO.Id; UserRoleDTO userRoleDTO = new UserRoleDTO() { UserId = userId, RoleId = 2 }; // add to db and save db.UserRoles.Add(userRoleDTO); db.SaveChanges(); } TempData["SM"] = "你已經創建帳戶!!"; // Redirect return(Redirect("~/user/login")); }
public void SetFeature(AccountFeature feature, bool value) { if (value) { m_Features |= feature; } else { m_Features &= ~feature; } }
public ActionResult UserProfile(UserProfileVM model) { if (!ModelState.IsValid) { return(View("UserProfile", model)); } // Check if password if need be if (!string.IsNullOrWhiteSpace(model.Password)) { if (!model.Password.Equals(model.ConfirmPassword)) { ModelState.AddModelError("", "Password not match"); return(View("UserProfile", model)); } } using (Db db = new Db()) { string userAccount = User.Identity.Name; AccountFeature accountFeature = new AccountFeature(); var hashPassword = accountFeature.HashPassword(model.Password); // Make sure account is unique if (db.Users.Where(x => x.Id != model.Id).Any(x => x.Account == userAccount)) { ModelState.AddModelError("", "Account " + model.Account + " already exists!"); model.Account = ""; return(View("UserProfile", model)); } // Edit UserDTO dto = db.Users.Find(model.Id); dto.Name = model.Name; dto.Email = model.Email; dto.Account = model.Account; if (!string.IsNullOrWhiteSpace(model.Password)) { dto.Password = hashPassword; } db.SaveChanges(); } TempData["SM"] = "You have edit your profile!"; return(Redirect("~/user/user-profile")); }
/// <summary> /// Deserializes an Account instance from an xml element. Intended only to be called from Utility.Load. /// </summary> /// <param name="node">The XmlElement instance from which to deserialize.</param> public Account( XmlElement node ) { m_Username = Utility.GetText( node["username"], "empty" ); string plainPassword = Utility.GetText( node["password"], null ); string cryptPassword = Utility.GetText( node["cryptPassword"], null ); string newCryptPassword = Utility.GetText( node["newCryptPassword"], null ); m_Features = (AccountFeature) Utility.GetInt32( Utility.GetText( node["features"], "0" ), 0 ); switch ( AccountHandler.ProtectPasswords ) { case PasswordProtection.None: { if ( plainPassword != null ) SetPassword( plainPassword ); else if ( newCryptPassword != null ) m_NewCryptPassword = newCryptPassword; else if ( cryptPassword != null ) m_CryptPassword = cryptPassword; else SetPassword( "empty" ); break; } case PasswordProtection.Crypt: { if ( cryptPassword != null ) m_CryptPassword = cryptPassword; else if ( plainPassword != null ) SetPassword( plainPassword ); else if ( newCryptPassword != null ) m_NewCryptPassword = newCryptPassword; else SetPassword( "empty" ); break; } default: // PasswordProtection.NewCrypt { if ( newCryptPassword != null ) m_NewCryptPassword = newCryptPassword; else if ( plainPassword != null ) SetPassword( plainPassword ); else if ( cryptPassword != null ) m_CryptPassword = cryptPassword; else SetPassword( "empty" ); break; } } m_AccessLevel = (AccessLevel) Enum.Parse( typeof( AccessLevel ), Utility.GetText( node["accessLevel"], "Player" ), true ); m_Flags = Utility.GetInt32( Utility.GetText( node["flags"], "0" ), 0 ); m_Created = Utility.GetDateTime( Utility.GetText( node["created"], null ), DateTime.Now ); m_LastLogin = Utility.GetDateTime( Utility.GetText( node["lastLogin"], null ), DateTime.Now ); m_Mobiles = LoadMobiles( node ); m_Comments = LoadComments( node ); m_Tags = LoadTags( node ); m_LoginIPs = LoadAddressList( node ); m_IPRestrictions = LoadAccessCheck( node ); for ( int i = 0; i < m_Mobiles.Length; ++i ) { if ( m_Mobiles[i] != null ) m_Mobiles[i].Account = this; } TimeSpan totalGameTime = Utility.GetTimeSpan( Utility.GetText( node["totalGameTime"], null ), TimeSpan.Zero ); if ( totalGameTime == TimeSpan.Zero ) { for ( int i = 0; i < m_Mobiles.Length; i++ ) { PlayerMobile m = m_Mobiles[i] as PlayerMobile; if ( m != null ) totalGameTime += m.GameTime; } } m_TotalGameTime = totalGameTime; if ( this.Young ) CheckYoung(); }
public bool FeatureEnabled( AccountFeature feature ) { return ( m_Features & feature ) != 0; }
public void SetFeature( AccountFeature feature, bool value ) { if ( value ) m_Features |= feature; else m_Features &= ~feature; }
public bool FeatureEnabled(AccountFeature feature) { return((m_Features & feature) != 0); }