public static LoginInfo Login(string loginName,string password) { LoginInfo loginInfo = null; password = EncryptHelper.Md5(password); loginName = loginName.Trim(); using (AccountEntities accountEntities = new AccountEntities()){ User user = accountEntities.User.FirstOrDefault(x => x.LoginName == loginName && x.Password == password && x.IsActive); if(user != null){ string ip = Fetch.UserIp; loginInfo = accountEntities.LoginInfo.FirstOrDefault(x => x.LoginName == loginName && x.ClientIP == ip); if(loginInfo != null) loginInfo.LastAccessTime = DateTime.Now; else{ loginInfo = new LoginInfo { LastAccessTime = DateTime.Now, LoginToken = Guid.NewGuid(), UserID = user.ID, LoginName = loginName, ClientIP = ip, BusinessPermissionString = "101,102,201,202,301,302,303,304,401,402,403", //暂时默认所有权限 CreateTime = DateTime.Now }; accountEntities.LoginInfo.Add(loginInfo); accountEntities.SaveChanges(); } } } return loginInfo; }
public static SignInResponseModel TrySignIn(string userName, string password) { string result = string.Empty; using (var context = new AccountEntities()) { var responseMessage = new ObjectParameter("responseMessage", typeof(string)); context.appSignIn(userName, password, responseMessage); result = Convert.ToString(responseMessage.Value); } var response = new SignInResponseModel(); if (result.Equals(_responseSuccess)) { response.isSuccess = true; response.Message = _responseSuccess; } if (result.Equals(_responseIncorrectLogin)) { response.isSuccess = false; response.Message = _responseIncorrectLogin; } if (result.Equals(_responseIncorrectPassword)) { response.isSuccess = false; response.Message = _responseIncorrectPassword; } return(response); }
public static IEnumerable <app_Users> GetUsers() { using (var context = new AccountEntities()) { return(context.app_Users.ToList()); } }
/// <summary> /// Update customer with new email /// </summary> /// <param name="customer">updated customer</param> /// <returns>True on success, false otherwise</returns> public bool Update(Customer customer) { bool flag = false; // update in customers table using (TravelExpertsEntities db = new TravelExpertsEntities()) { // get customer from Customer table by phone number var cust = db.Customers.SingleOrDefault(c => c.CustBusPhone == customer.CustBusPhone); if (cust != null) // found customer { cust.CustEmail = customer.CustEmail; db.SaveChanges(); flag = true; } } // update in accounts table using (AccountEntities db = new AccountEntities()) { // get account var account = db.AspNetUsers.SingleOrDefault(accnt => accnt.PhoneNumber == customer.CustBusPhone); if (account != null) // found account { if (flag) // make sure customers table update succesfully { account.Email = customer.CustEmail; db.SaveChanges(); return(true); } } return(false); // one or both failed } }
/// <summary> /// Find an account by user id /// </summary> /// <param name="userId">identification for account</param> /// <returns>The email of the account</returns> public static string GetEmailInAccount(string userId) { using (AccountEntities db = new AccountEntities()) { return(db.AspNetUsers.SingleOrDefault(accnt => accnt.Id == userId).Email); } }
/// <summary> /// See if user name is free to use /// case insensitive /// </summary> /// <param name="userName">user name to check</param> /// <returns>True if free to use, otherwise false</returns> public static bool IsUniqueUserName(string userName) { using (AccountEntities db = new AccountEntities()) { var taken = db.AspNetUsers.SingleOrDefault(accnt => accnt.UserName.ToLower() == userName.ToLower()); return(taken == null); } }
public accWebSite GetWebSiteByDomain(string Domain, string ConnectionStringEntity) { AccountEntities account = new AccountEntities(ConnectionStringEntity); IEnumerable <Account.accWebSite> queryWebSite; queryWebSite = from ws in account.accWebSites where ws.Domain.Equals(Domain) select ws; return(queryWebSite.FirstOrDefault()); }
/// <summary> /// See if a customer that's in the database, a login account /// check against phone number in Customer table AND email in AspNetUsers table /// </summary> /// <param name="customer">Customer object to check</param> /// <returns>True if account exists, false otherwise</returns> public static bool AccountExists(Customer customer) { if (CustomerExists(customer)) // customer exists in Customer table - if customer is not in customer table, they can't be in accounts table { using (AccountEntities accntDB = new AccountEntities()) { // find account by email var accntResult = accntDB.AspNetUsers.SingleOrDefault(accnt => accnt.Email == customer.CustEmail); return(accntResult != null); // customer exists in customer table and has a login account in AspNetUsers table } } return(false); }
/// <summary> /// See if there's already an account linked to this email, case insensitive /// </summary> /// <param name="custEmail"></param> /// <returns>true if no account is linked with email, false otherwise</returns> public static bool IsUniqueEmail(string custEmail, out string error) { using (AccountEntities db = new AccountEntities()) { var taken = db.AspNetUsers.SingleOrDefault(accnt => accnt.Email.ToLower() == custEmail.ToLower()); if (taken == null) { error = ""; return(true); } error = "An account already exists with this email."; return(false); } }
/// <summary> /// Update a customer's user name in AspNetUsers table AND Customers table /// </summary> /// <param name="newCustomer">Customer object to update in database</param> /// <returns>True on success, false otherwise</returns> public static bool UpdateAccountUserName(Customer newCustomer) { // update accounts table using (AccountEntities db = new AccountEntities()) { // get account from AspNetUsers table by email var account = db.AspNetUsers.SingleOrDefault(accnt => accnt.Email == newCustomer.CustEmail); if (account != null) // found account { account.UserName = newCustomer.UserName; db.SaveChanges(); return(true); } return(false); } }
/// <summary> /// See if phone number is unique in Accounts table /// </summary> /// <param name="custPhone"></param> /// <param name="error"></param> /// <returns></returns> public static bool IsUniquePhone(string custPhone, out string error) { error = ""; using (AccountEntities db = new AccountEntities()) { var taken = db.AspNetUsers.SingleOrDefault(cust => cust.PhoneNumber == custPhone); if (taken == null) { return(true); } error = "An account is already linked to this phone number."; return(false); } }
public static void AccountOrderEFTest() { using (var context = new AccountEntities()) { //删除之前的测试数据 context.Database.ExecuteSqlCommand("delete from chapter3.[order]"); context.Database.ExecuteSqlCommand("delete from chapter3.account"); //添加新的测试数据 var account1 = new Account { City = "Raytown", State = "MO" }; account1.Orders.Add(new Order { Amount = 223.09M, ShipCity = "Raytown", ShipState = "MO" }); account1.Orders.Add(new Order { Amount = 189.32M, ShipCity = "Olathe", ShipState = "KS" }); var account2 = new Account { City = "Kansas City", State = "MO" }; account2.Orders.Add(new Order { Amount = 99.29M, ShipCity = "Kansas City", ShipState = "MO" }); var account3 = new Account { City = "North Kansas City", State = "MO" }; account3.Orders.Add(new Order { Amount = 102.29M, ShipCity = "Overland Park", ShipState = "KS" }); context.Accounts.Add(account1); context.Accounts.Add(account2); context.Accounts.Add(account3); context.SaveChanges(); } using (var context = new AccountEntities()) { var orders = from o in context.Orders join a in context.Accounts on new { Id = o.AccountId, City = o.ShipCity, State = o.ShipState } equals new { Id = a.AccountId, City = a.City, State = a.State } select o; Console.WriteLine("Orders shipped to the account's city, state..."); foreach (var order in orders) { Console.WriteLine("\tOrder {0} for {1}", order.AccountId.ToString(), order.Amount.ToString()); } } }
/// <summary>保存验证码</summary> public static Guid SaveVerifyCode(string verifyCodeText) { if(string.IsNullOrWhiteSpace(verifyCodeText)) throw new Exception("输入的验证码不能为空!"); using (AccountEntities dbContext = new AccountEntities()){ //CreateTime不可空,虽然有默认值,但Linq要显式传入时间,否则会变成NULL而出错 VerifyCode verifyCode = new VerifyCode { VerifyText = verifyCodeText, Guid = Guid.NewGuid(), CreateTime = DateTime.Now }; dbContext.VerifyCode.Add(verifyCode); dbContext.SaveChanges(); //不加Try/Catch的话不会弹出错误 return verifyCode.Guid; } }
/// <summary>检查验证码</summary> public static bool CheckVerifyCode(string verifyCodeText,Guid guid) { using (AccountEntities accountEntities = new AccountEntities()){ VerifyCode verifyCode = accountEntities.VerifyCode.FirstOrDefault(x => x.Guid == guid && x.VerifyText == verifyCodeText); if(verifyCode != null){ //验证成功后删除本条验证码 accountEntities.VerifyCode.Remove(verifyCode); accountEntities.SaveChanges(); //清除验证码大于2分钟还没请求的 DateTime expiredTime = DateTime.Now.AddMinutes(-2); accountEntities.VerifyCode.Where(x => x.CreateTime < expiredTime).Delete(); //Extend扩展方法 return true; } return false; } }