Guest User

Untitled

a guest
Oct 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. <authentication mode="Forms">
  2. <forms loginUrl="/Home/Login" timeout="15" />
  3. </authentication>
  4.  
  5. protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
  6. {
  7. var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
  8. if (authCookie != null)
  9. {
  10. FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
  11. if (authTicket != null && !authTicket.Expired)
  12. {
  13. var roles = authTicket.UserData.Split(',');
  14. HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(authTicket), roles);
  15. }
  16. }
  17. }
  18.  
  19. public ActionResult Login()
  20. {
  21. return View();
  22. }
  23.  
  24. [HttpPost]
  25. public ActionResult Login(string email, string senha, string ReturnUrl)
  26. {
  27. Pessoas usuarios = db.Pessoas.Where(t => t.Email == email && t.Senha == senha).ToList().FirstOrDefault();
  28. if (usuarios != null)
  29. {
  30. string permissoes = "";
  31. permissoes += usuarios.TipoUsuario + ",";
  32. permissoes = permissoes.Substring(0, permissoes.Length - 1);
  33. FormsAuthentication.SetAuthCookie(usuarios.Nome, false);
  34. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, usuarios.Email, DateTime.Now, DateTime.Now.AddMinutes(30), false, permissoes);
  35. string hash = FormsAuthentication.Encrypt(ticket);
  36. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
  37. if (ticket.IsPersistent)
  38. {
  39. cookie.Expires = ticket.Expiration;
  40. }
  41. Response.Cookies.Add(cookie);
  42. if (String.IsNullOrEmpty(ReturnUrl))
  43. {
  44. return RedirectToAction("Index", "Home");
  45. }
  46. else
  47. {
  48. var decodedUrl = Server.UrlDecode(ReturnUrl);
  49. if (Url.IsLocalUrl(decodedUrl))
  50. {
  51. return Redirect(decodedUrl);
  52. }
  53. else
  54. {
  55. return RedirectToAction("Index", "Home");
  56. }
  57. }
  58. }
  59. else
  60. {
  61. ModelState.AddModelError("", "E-mail ou Senha estão incorretos");
  62. return View();
  63. }
  64. }
  65.  
  66. @{
  67. ViewBag.Title = "Login";
  68. }
  69.  
  70. @using (Html.BeginForm())
  71. {
  72. @Html.AntiForgeryToken()
  73. <div class="container conteudo">
  74. <form>
  75. <div class="row">
  76. <div class="form-group col-md-8 offset-md-2 col-lg-6 offset-lg-3">
  77. <label>Email</label>
  78. <input type="email" class="form-control" id="email" name="email" placeholder="Email" required="required">
  79. </div>
  80. </div>
  81. <div class="row">
  82. <div class="form-group col-md-8 offset-md-2 col-lg-6 offset-lg-3">
  83. <label>Senha</label>
  84. <input type="password" class="form-control" id="senha" value="" name="senha" placeholder="Senha" required="required">
  85. </div>
  86. </div>
  87. <div class="row">
  88. <div class="form-group col-md-8 offset-md-2 col-lg-6 offset-lg-3">
  89. <button type="submit" class="btn btn-primary btn-lg btn-block">Entrar</button>
  90. </div>
  91. </div>
  92. <div class="row">
  93. <div class="form-group col-sm-6 offset-sm-3">
  94. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  95. </div>
  96. </div>
  97. </form>
  98. </div>
  99. }
Add Comment
Please, Sign In to add comment