Guest User

Untitled

a guest
Nov 26th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. public void Main()
  2. {
  3. HttpWebRequest endpointRequest =
  4. (HttpWebRequest)HttpWebRequest.Create("https://domainname.sharepoint.com/_api/web/currentuser/?$expand=groups");
  5. endpointRequest.Method = "GET";
  6. endpointRequest.Accept = "application/json;odata=verbose";
  7. NetworkCredential cred = new System.Net.NetworkCredential("userName", "Password", "domain name");
  8. endpointRequest.Credentials = cred;
  9.  
  10.  
  11. try
  12. {
  13. HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
  14. WebResponse webResponse = endpointRequest.GetResponse();
  15. Stream webStream = webResponse.GetResponseStream();
  16. StreamReader responseReader = new StreamReader(webStream);
  17. string response = responseReader.ReadToEnd();
  18. MessageBox.Show(response);
  19. }
  20. catch (Exception e)
  21. {
  22. MessageBox.Show(e.Message);
  23. //Console.Out.WriteLine(e.Message); Console.ReadLine();
  24. }
  25.  
  26. }
  27.  
  28. string siteUrl = "https://tenant.sharepoint.com/sites/developer/";
  29. string userName = "lee@tenant.onmicrosoft.com";
  30. string password = "password";
  31.  
  32. using (var ctx = new ClientContext(siteUrl))
  33. {
  34. SecureString securePassword = new SecureString();
  35. foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
  36. ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
  37.  
  38. string sHtml = "";
  39. HttpWebRequest getRequest;
  40. HttpWebResponse response = null;
  41. Stream stream = null;
  42.  
  43. var uri = new Uri("https://tenant.sharepoint.com/sites/Developer/_api/web/currentuser/?$expand=groups");
  44. var credentials = new SharePointOnlineCredentials(userName, securePassword);
  45. var authCookie = credentials.GetAuthenticationCookie(uri);
  46. var cookieContainer = new CookieContainer();
  47. cookieContainer.SetCookies(uri, authCookie);
  48.  
  49. getRequest = (HttpWebRequest)WebRequest.Create(uri);
  50. getRequest.CookieContainer = cookieContainer;
  51. getRequest.Credentials = credentials;
  52. getRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
  53. getRequest.Method = "GET";
  54. getRequest.Accept = "application/json;odata=verbose";
  55.  
  56. response = (HttpWebResponse)getRequest.GetResponse();
  57. stream = response.GetResponseStream();
  58. StreamReader responseReader = new StreamReader(stream);
  59. string _Response = responseReader.ReadToEnd();
  60. Console.ReadKey();
  61. }
Add Comment
Please, Sign In to add comment