Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. //Accounts controller
  2. [HttpPost]
  3. public ActionResult Login(LoginViewModel model, string returnUrl)
  4. {
  5.  
  6. string domain = (string)model.domain
  7. string userName = (string)model.UserName;
  8. string password = (string)model.Password;
  9.  
  10. try
  11. {
  12. DirectoryEntry entry = new DirectoryEntry();
  13. switch (domain)
  14. {
  15. case "OPTION1":
  16. entry = new DirectoryEntry("LDAP://xx.xx.xx.xx:389", userName, password);
  17. break;
  18. case "OPTION2":
  19. entry = new DirectoryEntry("LDAP://yy.yy.yy.yy:389", userName, password);
  20. break;
  21.  
  22. }
  23. object nativeObject = entry.NativeObject;
  24. FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
  25. if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
  26. && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\"))
  27. {
  28. return this.Redirect(returnUrl);
  29. }
  30. return this.RedirectToAction("Index", "Home");
  31.  
  32.  
  33. }
  34. catch (Exception ex)
  35. {
  36.  
  37. this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");
  38. }
  39. return View(model);
  40. }
  41.  
  42. //web.config
  43. <authentication mode="Forms">
  44. <forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="60" slidingExpiration="false" protection="All" />
  45. </authentication>
  46.  
  47.  
  48. //in AccountViewModels I have
  49. using System.ComponentModel.DataAnnotations;
  50.  
  51. public class LoginViewModel
  52. {
  53. [Required]
  54. [Display(Name = "User name")]
  55. public string UserName { get; set; }
  56.  
  57. [Required]
  58. [DataType(DataType.Password)]
  59. [Display(Name = "Password")]
  60. public string Password { get; set; }
  61.  
  62. [Display(Name = "Remember me?")]
  63. public bool RememberMe { get; set; }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement