我有Azure上託管的Rest服務,並且Azure AD身份驗證已啓用。我在使用Microsoft Add這個Web服務。WebAPI使用用戶名和密碼對Azure AD進行非交互式驗證
我不想提示Microsoft登錄頁面給用戶。請用他的用戶名和密碼登陸,並獲取訪問令牌和刷新令牌。
我已經使用以下代碼:
private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
AuthenticationResult result = null;
string user = "";
string password = "";
string resrouce = "web service URL";
authContext = new AuthenticationContext(authority, new FileCache());
UserCredential uc = new UserPasswordCredential(user, password);
result = authContext.AcquireTokenAsync(todoListResourceId, clientId, uc).Result;
例外此代碼是「請求體必須包含以下參數:client_secret或client_assertion」。我沒有找到一種方法將用戶憑據傳遞給客戶端機密或客戶端斷言。
我也試過下面的代碼。在下面的代碼中,使用客戶端ID和客戶端密鑰並獲取將在一小時內過期的訪問令牌,並且此代碼不會返回刷新令牌。該訪問令牌不是特定於用戶的。
string tenantName = "contoso.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
string clientId = "a3b4eef4-33f0-4e98-9d57-ad14549bf310";
string key = "Secret Key";
ClientCredential clientCred = new ClientCredential(clientId, key);
string resource = "Service URL ";
string token;
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(resource, clientCred).Result;
token = authenticationResult.AccessToken;
有沒有什麼辦法讓從Azure的AD訪問令牌和刷新令牌不提示用戶的Microsoft登錄表單。
是的,我已經使用客戶端憑證流程。並能夠獲得訪問令牌。我希望我會同時與多個用戶合作 –