How to custome login and create cookies in mvc 4
Custom login and create cookies in mvc 4. Cookies as timeout time and store in encrypt format
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Surendra KandiraRank #3,063 · 462 pointsPosted Jul 15, 2015, 12:53 AM EDT
{
///var macAddress = db.MacAddresses.FirstOrDefault(a => a.Username.Equals(model.UserName) && a.isActive == true);
if (Membership.ValidateUser(model.UserName, model.Password) && ModelState.IsValid)
{
//if (macAddress != null && !GetMACAddress().Equals(macAddress.MacAddress1.Replace("-","")))
// {
// ViewBag.Message = "Wrong You are not allow on this system, Try again another system";
//}
//else {
int id = Convert.ToInt32(model.UserName);
var prefence = new Preference();
prefence = db.Preferences.Where(x => x.EmployeeId == id).FirstOrDefault();
HttpCookie cookie = Request.Cookies["DropOrder"];
if (model.RememberMe == true)
{
int timeout = model.RememberMe ? 525600 : 30;
var ticket = new FormsAuthenticationTicket(model.UserName, model.RememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie1.Expires = System.DateTime.Now.AddMinutes(timeout);//My Line
cookie1.HttpOnly = true;
HttpContext.Response.Cookies.Add(cookie1);
//FormsAuth.SetAuthCookie
}
var response = HttpContext.Response;
if (Request.Cookies["DropOrder"] != null)
{
response.Cookies.Remove("DropOrder");
}
string str = "";
if (prefence == null)
{
var prefence1 = new Preference();
prefence1.EmployeeId = id;
prefence1.OrderPreference = "orderName";
db.Preferences.Add(prefence1);
db.SaveChanges();
str = "orderName";
}
else
{
str = prefence.OrderPreference;
}
var DropOrder = new HttpCookie("DropOrder");
DropOrder.Value = str;
DropOrder.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(DropOrder);
FormsAuthentication.SetAuthCookie(model.UserName, false);
if (Url.IsLocalUrl(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Users", "Home");
}
//}
else
{
ViewBag.Message = "Wrong UserName/Password, Try again";
}
return View();
}
Vijay96 pointsPosted Jul 15, 2015, 1:22 AM EDT
Surendra KandiraRank #3,063 · 462 pointsPosted Aug 31, 2015, 4:48 AM EDT