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
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Surendra KandiraPosted Aug 31, 2015, 8:48 AM
VijayPosted Jul 15, 2015, 5:22 AM
Surendra KandiraPosted Jul 15, 2015, 4:53 AM
{
///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();
}