Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class LoginService
  2. {
  3. public async Task Login(string username, string password)
  4. {
  5. HttpWebRequest request = new HttpWebRequest(new Uri(String.Format("{0}Token", Constants.BaseAddress)));
  6. request.Method = "POST";
  7.  
  8. string postString = String.Format("username={0}&password={1}&grant_type=password",
  9. HttpUtility.HtmlEncode(username), HttpUtility.HtmlEncode(password));
  10. byte[] bytes = Encoding.UTF8.GetBytes(postString);
  11. using (Stream requestStream = await request.GetRequestStreamAsync())
  12. {
  13. requestStream.Write(bytes, 0, bytes.Length);
  14. }
  15.  
  16. try
  17. {
  18. HttpWebResponse httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
  19. string json;
  20. using (Stream responseStream = httpResponse.GetResponseStream())
  21. {
  22. json = new StreamReader(responseStream).ReadToEnd();
  23. }
  24. TokenResponseModel tokenResponse = JsonConvert.DeserializeObject(json);
  25. return tokenResponse.AccessToken;
  26. }
  27. catch (Exception ex)
  28. {
  29. throw new SecurityException("Bad credentials", ex);
  30. }
  31. }
  32. }
  33.  
  34. Application.Current.Properties ["token"] = myToken;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement