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 (FormsAuthDBEntities dc = new FormsAuthDBEntities()) { roles = (from a in dc.Roles join b in dc.UserRoles on a.RoleId equals b.RoleID join c in dc.UserDetails on b.UserID equals c.UserId where c.UserName.Equals(username) select a.RoleName).ToArray <string>(); if (roles.Count() > 0) { HttpRuntime.Cache.Insert (cacheKey, roles, null, DateTime.Now.AddMinutes (_cacheTimeoutInMinute), Cache.NoSlidingExpiration); } } return(roles); }
public ActionResult Login(Login l, string ReturnUrl = "/") { if (ModelState.IsValid) { MyMembershipProvider membership = new MyMembershipProvider(); bool isValidUser = membership.ValidateUser(l.Username, l.Password); if (isValidUser) { UserDetail user = null; using (FormsAuthDBEntities dc = new FormsAuthDBEntities()) { user = dc.UserDetails.Where(a => a.UserName.Equals(l.Username)).FirstOrDefault(); } if (user != null) { JavaScriptSerializer js = new JavaScriptSerializer(); string data = js.Serialize(user); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), l.RememberMe, data); string encToken = FormsAuthentication.Encrypt(ticket); HttpCookie authoCookies = new HttpCookie(FormsAuthentication.FormsCookieName, encToken); Response.Cookies.Add(authoCookies); return(Redirect(ReturnUrl)); } } } ModelState.Remove("Password"); return(View()); }
public override bool ValidateUser(string username, string password) { using (FormsAuthDBEntities db = new FormsAuthDBEntities()) { var user = db.UserDetails.Where(a => a.UserName.Equals(username) && a.Password.Equals(password)) .FirstOrDefault(); if (user != null) { return(true); } } return(false); }