CSharp/SPO.Samples/SPO.ModernAuthentication/Helpers/UserAuthenticationManager.cs
/// <summary>
/// Method to get SPO Context.
/// </summary>
/// <param name="web">SPO Web Url.</param>
/// <param name="userPrincipalName">UserName.</param>
/// <param name="userPassword">Password.</param>
/// <param name="clientId">AAD App Client Id.</param>
/// <returns>SPO Client Context.</returns>
Method to get SPO Context.
Method to get SPO Context .
public ClientContext GetContext(Uri web, string userPrincipalName, SecureString userPassword, string clientId)
        {
            var context = new ClientContext(web);
            context.ExecutingWebRequest += (sender, e) =>
            {
                string accessToken = this.EnsureAccessTokenAsync(new Uri($"{web.Scheme}://{web.DnsSafeHost}"), userPrincipalName, new System.Net.NetworkCredential(string.Empty, userPassword).Password, clientId).GetAwaiter().GetResult();
                e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accessToken;
            };
            return context;
        }
public ClientContext GetContext ( Uri web , string userPrincipalName , SecureString userPassword , string clientId ) { var context = new ClientContext ( web ) ; context . ExecutingWebRequest += ( sender , e ) => { string accessToken = this . EnsureAccessTokenAsync ( new Uri ( $" { web . Scheme } :// { web . DnsSafeHost } " ) , userPrincipalName , new System . Net . NetworkCredential ( string . Empty , userPassword ) . Password , clientId ) . GetAwaiter ( ) . GetResult ( ) ; e . WebRequestExecutor . RequestHeaders [ " " ] = " " + accessToken ; } ; return context ; }
Method to get SPO Context.
Method to get SPO Context .
SPO Client Context.
SPO Client Context .
web
Uri
SPO Web Url.
SPO Web Url .
userPrincipalName
string
None
userPassword
SecureString
None
clientId
string
AAD App Client Id.
AAD App Client Id .