Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult Login(Login login)
  3. {
  4.  
  5. if (!ModelState.IsValid) //Check for validation errors
  6. {
  7. Response.StatusCode = (int)HttpStatusCode.BadRequest;
  8. return PartialView("_login", login);
  9. }
  10.  
  11. ActiveDirectoryDAL.ADQueries ad = new ActiveDirectoryDAL.ADQueries();
  12. var userName = login.UserName.ToLower().Trim();
  13. var password = login.Password.Trim();
  14.  
  15.  
  16. var isValidUser = ad.Authenticate(userName, password);
  17.  
  18.  
  19. if (!isValidUser)
  20. {
  21. ModelState.AddModelError(string.Empty, "Login failed");
  22. return PartialView("_login", login);
  23. }
  24.  
  25. FormsAuthentication.SetAuthCookie(login.UserName, false);
  26. return Json(new { RedirectUrl = Url.Action("Index", "Home") });
  27.  
  28. }
  29.  
  30. @model PushNotificationWebSite.Models.Login
  31. @{
  32. ViewBag.Title = "Login";
  33. Layout = "";
  34. }
  35.  
  36.  
  37. <html>
  38. <head>
  39. <link href="~/Content/login.css" rel="stylesheet" />
  40. @Styles.Render("~/Content/bootstrap/css")
  41. @Scripts.Render("~/bundles/modernizr")
  42. @Scripts.Render("~/bundles/jquery")
  43. @Scripts.Render("~/bundles/bootstrap")
  44. </head>
  45. <script type="text/javascript">
  46. function OnSuccess(data) {
  47. if (data.RedirectUrl)
  48. window.location.href = data.RedirectUrl;
  49. }
  50. function OnLoginFailure(data) {
  51. debugger;
  52. $('#login').html(data.responseText);
  53.  
  54. }
  55. </script>
  56.  
  57. <body id="LoginForm">
  58. <div class="container">
  59. <h1 class="form-heading">login Form</h1>
  60. <div class="login-form">
  61. <div class="main-div">
  62. <div class="panel">
  63. <h2>Admin Login</h2>
  64. <p>Please enter your username and password</p>
  65. </div>
  66. @using (Ajax.BeginForm("Login", "Account", new AjaxOptions
  67. {
  68. InsertionMode = InsertionMode.Replace,
  69. HttpMethod = "POST",
  70. LoadingElementId = "loader",
  71. OnSuccess = "OnSuccess",
  72. OnFailure = "OnLoginFailure"
  73.  
  74.  
  75. }, new { id = "login" }))
  76. {
  77. @Html.ValidationSummary(true)
  78. <div class="form-group">
  79.  
  80. @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", autocomplete = "off", placeholder = "Username" })
  81.  
  82.  
  83. </div>
  84. <div class="form-group">
  85.  
  86. @Html.TextBoxFor(model => model.Password, new { @class = "form-control", autocomplete = "off", type = "password", placeholder = "Password" })
  87. @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
  88.  
  89. </div>
  90.  
  91. <button type="submit" class="btn btn-primary">Login</button>
  92. }
  93. <div id="loader" class="loader">
  94. <img class="center" src="~/Images/ajax-loader.gif" />
  95. </div>
  96. </div>
  97.  
  98. </div>
  99. </div>
  100. </body>
  101. </html>
  102.  
  103. @model PushNotificationWebSite.Models.Login
  104.  
  105. @using (Ajax.BeginForm("Login", "Account", new AjaxOptions
  106. {
  107. HttpMethod = "POST",
  108. LoadingElementId = "loader",
  109. OnSuccess = "OnSuccess",
  110. OnFailure = "OnLoginFailure"
  111.  
  112.  
  113. }, new { id = "login" }))
  114. {
  115. @Html.ValidationSummary(true)
  116. <div class="form-group">
  117.  
  118. @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", autocomplete = "off", placeholder = "Username" })
  119. @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
  120.  
  121. </div>
  122. <div class="form-group">
  123.  
  124. @Html.TextBoxFor(model => model.Password, new { @class = "form-control", autocomplete = "off", type = "password", placeholder = "Password" })
  125. @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
  126.  
  127. </div>
  128. <button type="submit" class="btn btn-primary">Login</button>
  129. }
  130.  
  131. ModelState.AddModelError(string.Empty, "Login failed");
  132.  
  133. ModelState.AddModelError("LoginFailed", "Login failed");
  134.  
  135. @* message without styling *@
  136. @Html.ValidationMessage("LoginFailed")
  137.  
  138. @* message with styling *@
  139. @Html.ValidationMessage("LoginFailed", new { @class = "text-danger" })
  140.  
  141. @ViewData.ModelState["LoginFailed"].Errors[0].ErrorMessage
  142.  
  143. @using (Ajax.BeginForm("Login", "Account", new AjaxOptions
  144. {
  145. InsertionMode = InsertionMode.Replace,
  146. HttpMethod = "POST",
  147. LoadingElementId = "loader",
  148. UpdateTargetId = "elementId", // here you can set HTML element ID to update
  149. OnSuccess = "OnSuccess",
  150. OnFailure = "OnLoginFailure"
  151. }, new { id = "login" }))
  152. {
  153. // form contents
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement