示例#1
0
 public UsersPage()
 {
     this.InitializeComponent();
     this.DataContext            = this;
     this.authenticationProvider = new MsalAuthenticationProvider();
     this.graphService           = new GraphServiceClient(authenticationProvider);
 }
示例#2
0
        public GraphServiceClient GetGraphClient()
        {
            const string clientId     = "SHOULD_BE_CONFIGURE"; //app regis Application (client) ID
            const string clientSecret = "SHOULD_BE_CONFIGURE";
            const string redirectUri  = "http://localhost:5001";



            const string tenantId  = "SHOULD_BE_CONFIGURE"; //Azure DirectoryId  => AD => Directory properties
            const string authority = "https://login.microsoftonline.com/" + tenantId;

            var appTokenCache = new GraphTokenCacheMemory(tenantId, _cache);

            var cca = new ConfidentialClientApplication(clientId,
                                                        authority, redirectUri, new ClientCredential(clientSecret), null, appTokenCache.GetCacheInstance());

            //// use the default permissions assigned from within the Azure AD app registration portal
            var scopes = new List <string> {
                "https://graph.microsoft.com/.default"
            };

            var authenticationProvider = new MsalAuthenticationProvider(tenantId, cca, scopes.ToArray());
            var graphClient            = new GraphServiceClient(authenticationProvider);

            return(graphClient);
        }
示例#3
0
        private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config, string userName, SecureString userPassword)
        {
            var clientId  = config["applicationId"];
            var authority = $"https://login.microsoftonline.com/{config["tenantId"]}/v2.0";

            List <string> scopes = new List <string>();

            scopes.Add("User.Read");
            scopes.Add("User.Read.All");

            var cca = PublicClientApplicationBuilder.Create(clientId)
                      .WithAuthority(authority)
                      .Build();

            return(MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray(), userName, userPassword));
        }
示例#4
0
    private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config)
    {
      var tenantId = config["tenantId"];
      var clientId = config["applicationId"];
      var clientSecret = config["applicationSecret"];
      var authority = $"https://login.microsoftonline.com/{config["tenantId"]}/v2.0";

      List<string> scopes = new List<string>();
      scopes.Add("https://graph.microsoft.com/.default");

      var cca = ConfidentialClientApplicationBuilder.Create(clientId)
                                              .WithAuthority(authority)
                                              .WithClientSecret(clientSecret)
                                              .Build();
      return MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray());
    }
示例#5
0
        private static IAuthenticationProvider CreateAuthorizationProvider(string tenantId, string appId, string userName, SecureString userPassword)
        {
            var clientId  = appId;// config["applicationId"];
            var authority = $"https://graph.microsoft.com/v1.0/{tenantId}/v2.0";

            List <string> scopes = new List <string>();

            scopes.Add("User.Read");
            scopes.Add("email");
            scopes.Add("profile");
            //scopes.Add("User.Read.All");

            var cca = PublicClientApplicationBuilder.Create(clientId)
                      .WithAuthority(authority)
                      .Build();

            return(MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray(), userName, userPassword));
        }
示例#6
0
        private static IAuthenticationProvider CreateAuthorizationProvider()
        {
            var tenantId     = "39076c6b-ee10-466c-b264-14a5350b127e";
            var clientId     = "c7c6f6ca-091d-4d29-8c17-db85228b1a66";
            var clientSecret = "g15tAZt38L6x6qRdzJ_7Rl9U1.pF-_aDYN";
            var authority    = "https://login.microsoftonline.com/39076c6b-ee10-466c-b264-14a5350b127e/v2.0";

            List <string> scopes = new List <string>();

            scopes.Add("https://graph.microsoft.com/.default");

            var cca = ConfidentialClientApplicationBuilder.Create(clientId)
                      .WithAuthority(authority)
                      .WithClientSecret(clientSecret)
                      .Build();

            return(MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray()));
        }
示例#7
0
        private string GetAuthenticationToken()
        {
            string token = string.Empty;

            var Authentication = System.Threading.Tasks.Task.Run(async() =>
            {
                IPublicClientApplication _clientApp = PublicClientApplicationBuilder
                                                      .Create(ApplicationSettings.General.MSALClientId)
                                                      .WithAuthority(AzureCloudInstance.AzurePublic, ApplicationSettings.General.MSALTenant)
                                                      .WithDefaultRedirectUri()
                                                      .Build();

                MsalTokenCacheHelper.EnableSerialization(_clientApp.UserTokenCache);

                #region Force Logoff (debug test)
                //var accounts = await _clientApp.GetAccountsAsync();
                //if (accounts.Any())
                //{
                //    try
                //    {
                //        await _clientApp.RemoveAsync(accounts.FirstOrDefault());
                //    }
                //    catch (MsalException ex)
                //    {
                //        Debug.WriteLine($"Error signing-out user: {ex.Message}");
                //    }
                //}
                #endregion

                List <string> scopes = new List <string>();
                scopes.Add("https://outlook.office.com/EWS.AccessAsUser.All");

                MsalAuthenticationProvider provider = new MsalAuthenticationProvider(_clientApp, scopes.ToArray());
                token = await provider.GetTokenAsync();
            });

            System.Threading.Tasks.Task.WaitAll(Authentication);

            return(token);
        }