Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. public ActionResult Login(string returnUrl)
  2. {
  3. ViewBag.ReturnUrl = returnUrl;
  4. return View();
  5. }
  6.  
  7. //
  8. // POST: /Account/Login
  9.  
  10. [HttpPost]
  11. [AllowAnonymous]
  12. [ValidateAntiForgeryToken]
  13. public ActionResult Login(LoginModel model, string returnUrl)
  14. {
  15. try
  16. {
  17. if (model.UserName == null && model.Password == null)
  18. {
  19. return View();
  20.  
  21. }
  22. else
  23. {
  24. var errors = ModelState
  25. .Where(x => x.Value.Errors.Count > 0)
  26. .Select(x => new { x.Key, x.Value.Errors })
  27. .ToArray();
  28. if (Membership.ValidateUser(model.UserName, model.Password))
  29. {
  30. if (ModelState.IsValid && model.UserName != null && model.Password != null && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
  31. {
  32. UsersContext usercontext = new UsersContext();
  33. var profile = usercontext.UserProfiles.FirstOrDefault(d => d.UserName.Equals(model.UserName));
  34. if (profile != null)
  35. {
  36. Session["LogedUserFirstName"] = profile.FirstName;
  37. Session["Email"] = profile.Email;
  38. Session["Username"] = profile.UserName;
  39. Session["LastName"] = profile.LastName;
  40. Session["MobileNumber"] = profile.MobileNumber;
  41. if (Roles.IsUserInRole(model.UserName, "Admin"))
  42. {
  43. return RedirectToAction("Index", "Base");
  44. }
  45. else if (Roles.IsUserInRole(model.UserName, "User"))
  46. {
  47. return RedirectToAction("myProfile");
  48.  
  49. }
  50. else {
  51.  
  52.  
  53. }
  54.  
  55. }
  56. else if (model.UserName == null && model.Password == null) { }
  57. else if (model.UserName == null && model.Password != null) { }
  58. else if (model.UserName != null && model.Password == null) { }
  59. else
  60. {
  61. ViewBag.Message("Username does not exist...");
  62. return View(model);
  63. }
  64.  
  65. }
  66. }
  67. else
  68. {
  69.  
  70. ViewBag.Message = "Username or Password provided is incorrect...";
  71. return View();
  72. }
  73.  
  74. }
  75.  
  76. }
  77. catch
  78. {
  79.  
  80. ViewBag.Message("OOPs.... Something went wrong");
  81. return View();
  82. }
  83.  
  84.  
  85. return View(model);
  86. }
  87.  
  88. @if (ViewData.ModelState.IsValid)
  89. {
  90. if (ViewBag.Message != null)
  91. {
  92. <h6 style="color:#ff0000;width:350px;">@ViewBag.Message</h6>
  93. }
  94. }
  95.  
  96. @using (Html.BeginForm("Login", "Account", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl }))
  97. {
  98. @Html.AntiForgeryToken()
  99. @Html.ValidationSummary(true)
  100.  
  101. <fieldset id="login_form">
  102. <label class="block clearfix" style="text-align: left">
  103. <span class="block input-icon input-icon-right"><i class="ace-icon fa fa-user"></i>
  104. @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", placeholder = "UserName" })
  105. <p id="log_valid">
  106. @Html.ValidationMessageFor(m => m.UserName)</p>
  107. </span>
  108. </label>
  109. <label class="block clearfix" style="text-align: left">
  110. <span class="block input-icon input-icon-right"><i class="ace-icon fa fa-lock"></i>
  111. @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
  112. <p id="log_valid">@Html.ValidationMessageFor(m => m.Password)</p>
  113. </span>
  114. </label>
  115. <div class="space" style="text-align: left">
  116. </div>
  117. <div class="clearfix">
  118. <label class="inline">
  119. <input type="checkbox" class="ace" checked= "@ViewBag.IsCheck" name="agreecheckbox" value="check" />
  120. <span class="lbl">Remember Me</span> <span>@ViewBag.Msg</span>
  121. </label>
  122. <button type="submit" class="width-35 pull-right btn btn-sm btn-primary" onclick="@("window.location.href='" + @Url.Action("AfterLogin", "Account"));" >
  123. <i class="ace-icon fa fa-key"></i><span class="bigger-110">Login</span>
  124. </button>
  125. <a href="@Url.Action("forgotPassword", "Account")">
  126. <h6>
  127. Forgot Password?</h6>
  128. </a>
  129. </div>
  130. </fieldset>
  131. }
  132. </div>
  133. <!-- /.widget-main -->
  134. </div>
  135. <!-- /.widget-body -->
  136. </div>
  137. <!-- /.login-box -->
  138. <div id="forgot-box" class="forgot-box widget-box no-border">
  139. <div class="widget-body">
  140. <div class="widget-main">
  141. <h4 class="header red lighter bigger">
  142. <i class="ace-icon fa fa-key"></i>Retrieve Password
  143. </h4>
  144. <div class="space-6">
  145. </div>
  146. <p>
  147. Enter your Username
  148. </p>
  149. <form>
  150. <fieldset>
  151. <label class="block clearfix">
  152. <span class="block input-icon input-icon-right">
  153. <input type="UserName" class="form-control" placeholder="Email" />
  154. <i class="ace-icon fa fa-user"></i></span>
  155. </label>
  156. <div class="clearfix">
  157. <button type="button" class="width-35 pull-right btn btn-sm btn-danger" onclick="location.href='@Url.Action("forgotPassword", "Account")'">
  158. Send Me <i class="ace-icon fa fa-lightbulb-o"></i>
  159. </button>
  160. </div>
  161. </fieldset>
  162. </form>
  163. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement