Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. RestUtils.GetTokenData(AppDefaults.UrlAPI, model.User, model.Pass);
  2.  
  3. Response.SetAuthCookie(model.User, true, model);
  4.  
  5. <system.web>
  6. ...
  7. <!-- Authentication -->
  8. <authentication mode="Forms">
  9. <forms loginUrl="~/Account/Login" slidingExpiration="true" timeout="30" />
  10. </authentication>
  11. <sessionState cookieless="UseCookies" mode="InProc" timeout="30" />
  12. </system.web>
  13.  
  14. public static class HttpResponseBaseExtensions
  15. {
  16. public static int SetAuthCookie<T>(this HttpResponseBase responseBase, string name, bool rememberMe, T userData)
  17. {
  18. HttpCookie cookie = FormsAuthentication.GetAuthCookie(name, rememberMe);
  19. FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
  20. if (ticket != null)
  21. {
  22. FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration,
  23. ticket.IsPersistent, JsonConvert.SerializeObject(userData), ticket.CookiePath);
  24. string encTicket = FormsAuthentication.Encrypt(newTicket);
  25. cookie.Value = encTicket;
  26. responseBase.Cookies.Add(cookie);
  27. return encTicket != null ? encTicket.Length : 0;
  28. }
  29. return 0;
  30. }
  31. }
  32.  
  33. public class LoginVM
  34. {
  35. public string User { get; set; }
  36. public string Pass { get; set; }
  37. }
  38.  
  39. public ActionResult Login(LoginVM model, string returnUrl)
  40. {
  41. ViewBag.ReturnUrl = returnUrl;
  42. if (ModelState.IsValid)
  43. {
  44. HttpContext.Current.Session["TokenData"] = RestUtils.GetTokenData(AppDefaults.UrlAPI, AppDefaults.ProxyConfig, model.User, model.Pass);
  45. Response.SetAuthCookie(model.User, true, model);
  46. return Redirect(returnUrl);
  47. }
  48. return View(model);
  49. }
  50.  
  51. public ActionResult LogOff()
  52. {
  53. if (HttpContext.Session != null)
  54. {
  55. HttpContext.Session.Clear();
  56. HttpContext.Session.Abandon();
  57. HttpContext.Session.RemoveAll();
  58. }
  59.  
  60. FormsAuthentication.SignOut();
  61. return RedirectToAction("Index", "Home");
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement