亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 WPF 應用程序檢查用戶是否存在于 Azure AD 中

從 WPF 應用程序檢查用戶是否存在于 Azure AD 中

C#
qq_笑_17 2023-06-25 13:40:52
我有一個 WPF 應用程序,其身份驗證是 AzureAD。如果有任何新用戶出現,我們首先會在 Azure AD 中添加該用戶,然后將同一用戶添加到我們的 WPF 應用程序中。將該用戶添加到 WPF 時,我們需要驗證該用戶是否存在于 Azure 中步驟 1. 網絡管理員在 Azure AD 中創建用戶 2. 我們的項目管理員將該用戶添加到 Azure 中的客戶端 3. 項目管理員使用 azure 身份驗證登錄到我們的 WPF 應用程序并添加此用戶 4. 此時我們需要重新檢查azureAD 中存在新用戶。這意味著項目管理員使用 Azure 身份驗證登錄到 WPF 應用程序 [他的用戶 ID、票證、客戶端 ID 等可用],并且他嘗試檢查用戶是否提供了 Azure AD [新用戶名可用,但該管理員不知道密碼]。請幫我編寫 c# 代碼來解決這個問題。
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

關于這個問題,請參考以下代碼


//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);


? ? ? ? }


查看完整回答
反對 回復 2023-06-25
  • 1 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號