實際上,任何客戶端應用程序都應該在AAD中進行註冊。
控制檯應用程序也必須在Azure AD中註冊,因此您將擁有客戶端ID和客戶端重定向url,但沒有客戶端祕密。然後將下面的代碼應工作:
void Main()
{
var clientId = "client-GUID";
var clientUrl = new Uri("redirect-url"); //can be anything starting with localhost
var tenant = "name of your AAD tenant";
string authority = "https://login.windows.net/" + tenant;
string resource = "https://outlook.office365.com"; ///put id or url of resource you're accessing
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
Console.WriteLine("Trying to acquire token");
var pp = new PlatformParameters(PromptBehavior.Auto); //this brings web prompt
var token = authenticationContext.AcquireTokenAsync(resource, clientId, clientUrl, pp, UserIdentifier.AnyUser).Result;
Console.WriteLine("Got the token: {0}", token.AccessToken);
}
單個頁面的js應用程序將需要在AD中註冊才能使用AD認證 – Justin
https://github.com/Azure-Samples/active-directory-dotnet-graphapi-安慰 ? – 4c74356b41
控制檯應用程序訪問受Azure AD保護的Web API的示例 - https://blogs.msdn.microsoft.com/benjaminperkins/2016/10/20/how-i-connected-a-console-application-to-一個幅材-API保護的通過-AN-天青活性目錄/ – alwayslearning