Guest User

Untitled

a guest
Jun 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. [HttpPost]
  2. [AllowAnonymous]
  3. [ValidateAntiForgeryToken]
  4. public JsonResult LoginClient(LoginModel model, string returnUrl)
  5. {
  6. if (ModelState.IsValid)
  7. {
  8. if (((SomethingMembershipProvider)Membership.Provider).ValidateUser(model.UserName, model.Password, ServiceType.Something))
  9. {
  10. FormsAuthentication.SetAuthCookie(model.UserName, false);
  11. return new JsonResult { Data = new { status = "ok" } };
  12. }
  13. else
  14. {
  15. ModelState.AddModelError("", "Feil brukernavn og/eller passord");
  16. }
  17. }
  18.  
  19.  
  20. return new JsonResult();
  21. }
  22.  
  23. username: string;
  24. password: string;
  25.  
  26. constructor(private http: HttpClient, private router: Router) { }
  27. verificationToken:string;
  28.  
  29. ngOnInit() {
  30.  
  31. this.onGetTokenClicked(); // I have a button inital for testing
  32. }
  33.  
  34. onLoginClicked(): void {
  35. let loginHeaders = new HttpHeaders({
  36. "Content-Type": "application/x-www-form-urlencoded"
  37. });
  38.  
  39. const body = new window.URLSearchParams();
  40. body.set("UserName", this.username);
  41. body.set("Password", this.password);
  42. body.set("__RequestVerificationToken", this.verificationToken);
  43.  
  44. this.http.post(`http://localhost:4200/Account/Home/LoginClient`, body.toString(), {
  45. headers: loginHeaders,
  46. withCredentials: true
  47. }).subscribe(s => {
  48. this.router.navigate(["/"]);
  49. });
  50. }
  51.  
  52. onGetTokenClicked(): void {
  53. this.http.get(`http://localhost:4200/Account/Home/Login`, {responseType: "text", withCredentials: true}).subscribe(t => {
  54. var parser = new DOMParser();
  55. var xmlDoc = parser.parseFromString(t, "text/html");
  56. var e:HTMLInputElement = xmlDoc.getElementsByName("__RequestVerificationToken").item(0) as HTMLInputElement;
  57. this.verificationToken = e.value;
  58. });
  59. }
Add Comment
Please, Sign In to add comment