public override string[] GetRolesForUser(string username) { if (!HttpContext.Current.User.Identity.IsAuthenticated) { return(null); } //check cache var cacheKey = string.Format("{0}_role", username); if (HttpRuntime.Cache[cacheKey] != null) { return((string[])HttpRuntime.Cache[cacheKey]); } string[] roles = new string[] { }; using (RegisterLionsEntities dc = new RegisterLionsEntities()) { roles = (from c in dc.TUsers where c.user_code.Equals(username) select c.role_name).ToArray <string>(); if (roles.Count() > 0) { HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration); } } return(roles); }
public static string chkUserCode(string first_name_eng, string last_name_eng) { RegisterLionsEntities db = new RegisterLionsEntities(); string user_code = ""; int j = 0; IEnumerable <TUser> chkuser = new List <TUser>(); do { if (j == 0) { user_code = first_name_eng.ToLower() + "." + last_name_eng.ToLower().Substring(0, 1); } else { user_code = first_name_eng.ToLower() + "." + last_name_eng.ToLower().Substring(0, 1) + last_name_eng.ToLower().Substring(j, 1); } chkuser = (from u1 in db.TUsers where u1.user_code == user_code select u1).ToList(); if (chkuser.Count() != 0) { j++; } } while (chkuser.Count() != 0); return(user_code); }
//private RegisterLionsEntities db = new RegisterLionsEntities(); public static void writeTransactionLog(int?pMemberSeq, string pViewName, int?pClub_ID) { ApplicationLog applicationLog = new ApplicationLog(); applicationLog.log_date = DateTime.Now; applicationLog.ip_addr = getIPAddress(); applicationLog.member_seq = pMemberSeq; applicationLog.view_name = pViewName; applicationLog.club_id = pClub_ID; RegisterLionsEntities db = new RegisterLionsEntities(); db.ApplicationLogs.Add(applicationLog); db.SaveChanges(); }
// Here In this example we will use only ValidateUser method, we will see remaining later like create user, //update user change password and more public override bool ValidateUser(string username, string password) { //Will write code for validate user from our own database using (RegisterLionsEntities dc = new RegisterLionsEntities()) { var strEncPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1"); // var strEncPwd = password; //var user = dc.TUsers.Where(a => a.user_code.Equals(username) && a.user_pwd.Equals(strEncPwd)).FirstOrDefault(); var user = (from u in dc.TUsers where u.user_code.Equals(username) && u.user_pwd.Equals(strEncPwd) join m in dc.Members on u.member_seq equals m.member_seq where m.member_sts == 1 select u ).ToList(); if (user.Count() == 1) { return(true); } } return(false); }
public ActionResult Login(Login login, string ReturnUrl = "") { #region Part 2 Code //if (ModelState.IsValid) //{ // var isValidUser = Membership.ValidateUser(loing.Username, loing.Password); // if (isValidUser) // { // FormsAuthentication.SetAuthCookie(loing.Username, loing.RememberMe); // if (Url.IsLocalUrl(ReturnUrl)) // { // return Redirect(ReturnUrl); // } // else // { // return RedirectToAction("Profile", "Home"); // } // } //} #endregion #region Part 4 Code if (ModelState.IsValid) { // ecncrypt before Validate or not if (!Membership.ValidateUser(login.Username, login.Password)) { ModelState.AddModelError("", "รหัสผู้ใช้/รหัสผ่าน ไม่ถูกต้อง"); return(View(login)); } //TUser user = null; //List<TUser> user; // string[][] baColl = new string[][] { }; RegisterLionsEntities db = new RegisterLionsEntities(); var user = (from u in db.TUsers join m in db.Members on u.member_seq equals m.member_seq join c in db.Clubs on m.club_id equals c.club_id join d in db.Districts on c.district_id equals d.district_id where u.user_code.Equals(login.Username) select new User { user_code = u.user_code, user_pwd = u.user_pwd, eff_date = u.eff_date, exp_date = u.exp_date, upd_date = u.upd_date, first_name = m.first_name, last_name = m.last_name, first_name_eng = m.first_name_eng, last_name_eng = m.last_name_eng, club_name_thai = c.club_name_thai, member_seq = u.member_seq, club_id = m.club_id, district_id = d.district_id, district_name_thai = d.district_name_thai }).FirstOrDefault(); //user = dc.TUsers.Where(a => a.user_code.Equals(login.Username)).FirstOrDefault(); //var user1 = (from c in dc.TUsers // where c.user_code.Equals(loing.Username)).FirstOrDefault(); if (user != null) { //var image = (from m in db.Members where m.member_seq.Equals(user.member_seq) // select Image.FromStream(new MemoryStream(m.image.ToArray() ))).FirstOrDefault(); // Write log to table TransactionLog //ProjLib projlib = new ProjLib(); ProjLib.writeTransactionLog(user.member_seq, "Login", user.club_id); JavaScriptSerializer js = new JavaScriptSerializer(); string data = js.Serialize(user); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.user_code, DateTime.Now, DateTime.Now.AddMinutes(30), login.RememberMe, data); string encToken = FormsAuthentication.Encrypt(ticket); HttpCookie authoCookies = new HttpCookie(FormsAuthentication.FormsCookieName, encToken); Response.Cookies.Add(authoCookies); if (Url.IsLocalUrl(ReturnUrl)) { return(Redirect(ReturnUrl)); } else if (user.upd_date != null) { return(RedirectToAction("Welcome", "Home")); } else { return(RedirectToAction("ChangePassword", "Manage")); } } } #endregion ModelState.Remove("Password"); return(View()); }