Advertisement
Guest User

Untitled

a guest
Jun 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult Login(LoginViewModel viewModel)
  3. {
  4. if (viewModel.Username == "alex" && viewModel.Password == "password")
  5. {
  6. FormsAuthentication.SetAuthCookie(viewModel.Username, false);
  7. return RedirectToAction("Index", "Home");
  8. }
  9.  
  10. return View();
  11. }
  12.  
  13. private static void GetPerson()
  14. {
  15. Console.WriteLine("Username:");
  16. string username = Console.ReadLine();
  17.  
  18. Console.WriteLine("Password:");
  19. string password = Console.ReadLine();
  20.  
  21. using (HttpClient httpClient = new HttpClient())
  22. {
  23. HttpRequestMessage authRequest = new HttpRequestMessage();
  24. authRequest.Method = HttpMethod.Post;
  25. authRequest.RequestUri = new Uri(@"http://localhost:4391/Account/Login");
  26. authRequest.Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
  27. {
  28. new KeyValuePair<string, string>("Username", username),
  29. new KeyValuePair<string, string>("Password", password)
  30. });
  31.  
  32. HttpResponseMessage authResponse = httpClient.SendAsync(authRequest).Result;
  33. IEnumerable<string> cookieValues;
  34. authResponse.Headers.TryGetValues("Set-Cookie", out cookieValues);
  35.  
  36. }
  37. }
  38.  
  39. HTTP/1.1 302 Found
  40. Cache-Control: private
  41. Content-Type: text/html; charset=utf-8
  42. Location: /
  43. Server: Microsoft-IIS/8.0
  44. X-AspNetMvc-Version: 5.1
  45. X-AspNet-Version: 4.0.30319
  46. Set-Cookie: .ASPXAUTH=A6A22208AECA1F3E25B43C834BE058A5019F3A9C55AED099FFAD5B0FE7B289EC2C3F87C157B0C1ED338D1DF0A6469E6C5DE8D9DB7A99D54D992EA10F26424BA579C262B7CD247CA4193879E058A233B7A0BC98E10503440B79EB988239C43696; path=/; HttpOnly
  47. X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcYWxleGFuZGVyXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTNcUHJvamVjdHNcRm9ybVZhbHVlc0F1dGhXZWJBUElcRm9ybVZhbHVlc0F1dGhXZWJBUElcQWNjb3VudFxMb2dpbg==?=
  48. X-Powered-By: ASP.NET
  49. Date: Sat, 19 Apr 2014 20:09:23 GMT
  50. Content-Length: 428
  51.  
  52. private static void GetPerson()
  53. {
  54. Console.WriteLine("Username:");
  55. string username = Console.ReadLine();
  56.  
  57. Console.WriteLine("Password:");
  58. string password = Console.ReadLine();
  59.  
  60. HttpClient httpClient = new HttpClient();
  61.  
  62. HttpRequestMessage authRequest = new HttpRequestMessage();
  63. authRequest.Method = HttpMethod.Post;
  64. authRequest.RequestUri = new Uri(@"http://localhost:4391/Account/Login");
  65. authRequest.Content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
  66. {
  67. new KeyValuePair<string, string>("Username", username),
  68. new KeyValuePair<string, string>("Password", password)
  69. });
  70.  
  71. HttpResponseMessage authResponse = httpClient.SendAsync(authRequest).Result;
  72.  
  73. HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, @"http://localhost:4391/api/Person/1");
  74. request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  75.  
  76. HttpResponseMessage response = httpClient.SendAsync(request).Result;
  77.  
  78. if (!response.IsSuccessStatusCode)
  79. {
  80. Console.WriteLine("Username or password is incorrect");
  81. return;
  82. }
  83.  
  84. response.Content.ReadAsAsync<Person>().ContinueWith((x) => {
  85. Person person = x.Result;
  86.  
  87. Console.WriteLine("First name: {0}", person.FirstName);
  88. Console.WriteLine("Last name: {0}", person.LastName);
  89. });
  90. }
  91.  
  92. [HttpPost]
  93. public ActionResult Login(string username, string password)
  94. {
  95. if (username == password)
  96. {
  97. FormsAuthentication.SetAuthCookie(username, true);
  98. return new HttpStatusCodeResult(HttpStatusCode.OK);
  99. }
  100. return new HttpUnauthorizedResult();
  101. }
  102.  
  103. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
  104. Response.Cookies.Add(cookie);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement