从WPF应用程序检查用户是否存在于Azure AD中

时间:2019-07-16 04:53:07

标签: c# azure-active-directory

我有一个WPF应用程序,其身份验证为AzureAD。如果有任何新用户出现,那么我们首先将其添加到Azure AD中,然后将同一用户添加到WPF应用程序中。在将该用户添加到WPF时,我们需要验证该用户是否在Azure中存在

步骤 1.网络管理员在Azure AD中创建用户 2.我们的项目管理员将该用户添加到Azure中的客户端 3.使用Azure身份验证并添加此用户将Project Admin登录到我们的WPF应用程序 4.那时我们需要重新检查azureAD中是否存在新用户。

这意味着项目管理员使用Azure身份验证登录到WPF应用程序[他的用户ID,票证,客户端ID等可用],并且他试图检查用户是否显示了它的Azure AD [新用户名可用,但是由此密码将不知道管理员]。

请帮助我编写一个C#代码来解决此问题。

1 个答案:

答案 0 :(得分:1)

关于此问题,请参考以下代码

//install Active Directory Authentication Library (ADAL) and Azure Active Directory Graph Client Library
    public static ActiveDirectoryClient GetActiveDirectoryClientAsApplication()
        {
            Uri servicePointUri = new Uri(ResourceId);
            Uri serviceRoot = new Uri(servicePointUri,tenant);
            ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
                async () => await AcquireTokenAsyncForApplication());
            return activeDirectoryClient;
        }

        public static async Task<string> AcquireTokenAsyncForApplication()
        {
            return await GetTokenForApplication();
        }

        /// <summary>
        /// Get Token for Application.
        /// </summary>
        /// <returns>Token for application.</returns>
        public static async Task<string> GetTokenForApplication()
        {

                AuthenticationContext authenticationContext = new AuthenticationContext(
                    authority,
                    false);

                // Configuration for OAuth client credentials 


                    ClientCredential clientCred = new ClientCredential(
                        clientId,
                        cred);
                    AuthenticationResult authenticationResult =
                        await authenticationContext.AcquireTokenAsync(ResourceId, clientCred);
                   var TokenForApplication = authenticationResult.AccessToken;


            return TokenForApplication;
        }

        public async Task<object> Getuser()
        {

            ActiveDirectoryClient client = GetActiveDirectoryClientAsApplication();
            var userLookupTask = client.Users.Where(
         user => user.UserPrincipalName.Equals(
        "", StringComparison.CurrentCultureIgnoreCase)).ExecuteSingleAsync();
            User result =(User) await userLookupTask;
            return result;

        }

        public async Task Adduser() {
            ActiveDirectoryClient client = GetActiveDirectoryClientAsApplication();
            var newUser = new User()
            {
                // Required settings
                DisplayName = "",
                UserPrincipalName = "",
                PasswordProfile = new PasswordProfile()
                {
                    Password = "H@ckMeNow!",
                    ForceChangePasswordNextLogin = false
                },
                MailNickname = "",
                AccountEnabled = true,

                // Some (not all) optional settings
                GivenName = "",
                Surname = "",
                JobTitle = "",
                Department = "",
                City = "",
                State = "",
                Mobile = "",
            };

            await client.Users.AddUserAsync(newUser);

        }

有关更多详细信息,请参阅https://blogs.msdn.microsoft.com/aadgraphteam/2014/12/11/announcing-azure-ad-graph-api-client-library-2-0/