private async void ExecuteLoginCommand() { Message = string.Empty; bool successfulAuth = false; IsLoading = true; try { // Attempt to sign the user into SharePoint Online using Integrated Windows Auth or username + password successfulAuth = await SharePointAuthentication.Create( new Uri(ServerUrl), Username, Userpassword, false); } catch (Exception) { } IsLoading = false; if (!successfulAuth) { Message = "Anmeldung fehlgeschlagen!"; return; } if (SaveUserCredentials) { var pwVault = new PasswordVault(); IReadOnlyList <PasswordCredential> credentials = null; // Find existing credentials try { credentials = pwVault.FindAllByResource(PasswordVaultName); // Remove existing credentials if (credentials != null) { foreach (PasswordCredential credential in credentials) { pwVault.Remove(credential); } } } catch { } var pwCredentials = new PasswordCredential(PasswordVaultName, Username, Userpassword); pwCredentials.Properties.Add(new KeyValuePair <string, object>("Url", ServerUrl)); pwVault.Add(pwCredentials); } Messenger.Default.Send <bool>(true, "ClosePopUp"); }
/// <summary> /// Sends a JSON OData request appending SPO auth cookies to the request header. /// </summary> /// <param name="uri">The request uri</param> /// <param name="method">The http method</param> /// <param name="requestContent">A stream containing the request content</param> /// <param name="clientHandler">The request client handler</param> /// <param name="authUtility">An instance of the auth helper to perform authenticated calls to SPO</param> /// <param name="headers">The http headers to append to the request</param> public static async Task <byte[]> SendODataJsonRequest(Uri uri, HttpMethod method, Stream requestContent, HttpClientHandler clientHandler, SharePointAuthentication authUtility, Dictionary <string, string> headers = null) { if (clientHandler.CookieContainer == null) { clientHandler.CookieContainer = new CookieContainer(); } CookieContainer cookieContainer = await authUtility.GetCookieContainer(); // get the auth cookies from SPO after authenticating with Microsoft Online Services STS foreach (Cookie c in cookieContainer.GetCookies(uri)) { clientHandler.CookieContainer.Add(uri, c); // apppend SPO auth cookies to the request } return(await SendHttpRequest( uri, method, requestContent, "application/json;odata=verbose;charset=utf-8", // the http content type for the JSON flavor of SP REST services clientHandler, headers)); }
/// <summary> /// Sends a JSON OData request appending the SharePoint canary to the request header. /// Appending the canary to the request is necessary to perform write operations (e.g. create, update, delete list items) /// The canary is a security measure to prevent cross site scripting attacks /// </summary> /// <param name="uri">The request uri</param> /// <param name="method">The http method</param> /// <param name="requestContent">A stream containing the request content</param> /// <param name="clientHandler">The request client handler</param> /// <param name="authUtility">An instance of the auth helper to perform authenticated calls to SPO</param> /// <returns></returns> public static async Task <byte[]> SendODataJsonRequestWithCanary(Uri uri, HttpMethod method, Stream requestContent, HttpClientHandler clientHandler, SharePointAuthentication authUtility) { // Make a post request to {siteUri}/_api/contextinfo to get the canary var response = await HttpUtility.SendODataJsonRequest( new Uri(String.Format("{0}/_api/contextinfo", SharePointAuthentication.Current.SiteUrl)), HttpMethod.Post, null, clientHandler, SharePointAuthentication.Current); Dictionary <String, IJsonValue> dict = new Dictionary <string, IJsonValue>(); HttpUtility.ParseJson(JsonObject.Parse(Encoding.UTF8.GetString(response, 0, response.Length)), dict); // parse the JSON response containing the canary string canary = dict["FormDigestValue"].GetString(); // the canary is contained in the FormDigestValue of the response body // Make the OData request passing the canary in the request headers return(await HttpUtility.SendODataJsonRequest( uri, method, requestContent, clientHandler, SharePointAuthentication.Current, new Dictionary <string, string> { { "X-RequestDigest", canary } })); }
public static ICredentials CreateCredentials(string UserName, string Password, SharePointAuthentication authticationType) { ICredentials result = null; switch (authticationType) { case SharePointAuthentication.SharePointActiveDirectory: result = new NetworkCredential(UserName, Password); break; case SharePointAuthentication.SharePointOnline: SecureString securePassword = new SecureString(); foreach (char c in Password) { securePassword.AppendChar(c); } result = new SharePointOnlineCredentials(UserName, securePassword); break; } return(result); }
public override async Task OnNavigatedTo() { if (!InternetHelpers.IsConnected) { return; } IsLoading = true; Message = "Suche vorhandene Anmeldedaten..."; bool successfulAuth = false; try { // Try to get the user's Office 365 credentials from the password vault var pwVault = new PasswordVault(); var creds = pwVault.FindAllByResource(PasswordVaultName); PasswordCredential credentials = creds.FirstOrDefault(); credentials.RetrievePassword(); Username = credentials.UserName; Userpassword = credentials.Password; Message = "Anmeldeversuch..."; if (!credentials.Properties.ContainsKey("Url")) { throw new Exception("Nothing found!"); } successfulAuth = await SharePointAuthentication.Create( new Uri(credentials.Properties["Url"].ToString()), credentials.UserName, credentials.Password, false); } catch (Exception) { } IsLoading = false; Message = string.Empty; if (successfulAuth) { Messenger.Default.Send <bool>(true, "ClosePopUp"); } try { IsLoading = true; Message = "Anmeldeversuch mit Systemkonto...."; Username = await UserInformation.GetPrincipalNameAsync(); Userpassword = string.Empty; // Try login with system user principals successfulAuth = await SharePointAuthentication.Create( new Uri(ServerUrl), Username, null, true); } catch (Exception) { } IsLoading = false; Message = string.Empty; Username = string.Empty; Userpassword = string.Empty; if (successfulAuth) { Messenger.Default.Send <bool>(true, "ClosePopUp"); } }