Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. //Here's my controller
  2. public class LoginController : ApiController
  3. [HttpPost]
  4. public object Login(Model.ViewModel.Login _cred)
  5. {
  6. var user = userAccountService.FindUser(_cred.UserName, _cred.Password);
  7. _cred.Password = "";
  8. if (user != null)
  9. {
  10. string baseAddress = "http://localhost:34914";
  11. using (var client = new HttpClient())
  12. {
  13. var form = new Dictionary<string, string>
  14. {
  15. {"grant_type", "password"},
  16. {"username", user.Username},
  17. {"password", user.Password},
  18. };
  19. var tokenResponse = client.PostAsync(baseAddress + "/token", new FormUrlEncodedContent(form)).Result;
  20. var token = tokenResponse.Content.ReadAsStringAsync().Result; //tokenResponse.Content.ReadAsAsync<Token>;
  21.  
  22. Model.ViewModel.Token returnToken = JsonConvert.DeserializeObject<Model.ViewModel.Token>(token);
  23. _cred.AccessToken = returnToken.Access_Token;
  24.  
  25. return _cred;
  26. }
  27. }
  28. else
  29. {
  30. var error = new Utility.JSONAPI.Error.JSONAPIError();
  31. error.Errors.Add(new Utility.JSONAPI.Error.ErrorObject()
  32. {
  33. Id = 1,
  34. Title = "Unauthorized",
  35. Detail = "Invalid username and password"
  36. });
  37. return Request.CreateResponse((HttpStatusCode)422, error);// (HttpStatusCode.InternalServerError, error);
  38. }
  39. }
  40. }
  41.  
  42.  
  43. //JSONAPI Class
  44. public class JSONAPIError
  45. {
  46. private List<ErrorObject> m_errors;
  47. public List<ErrorObject> Errors
  48. {
  49. get
  50. {
  51. if (m_errors == null) m_errors = new List<ErrorObject>();
  52. return m_errors;
  53. }
  54. }
  55. }
  56.  
  57.  
  58. public class ErrorObject
  59. {
  60. public int Id { get; set; }
  61. public ErrorLinksObject Links { get; set; }
  62. public string Status { get; set; }
  63. public string Code { get; set; }
  64. public string Title { get; set; }
  65. public string Detail { get; set; }
  66. public SourceObject Source { get; set; }
  67. public object Meta { get; set; }
  68. }
  69.  
  70. public class ErrorLinksObject
  71. {
  72. public ErrorLinkObject About { get; set; }
  73. }
  74.  
  75. public class ErrorLinkObject
  76. {
  77. public string Href { get; set; }
  78. public object Meta { get; set; }
  79. }
  80.  
  81. public class SourceObject
  82. {
  83. public string Pointer { get; set; }
  84. public string Parameter { get; set; }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement