可以将AzureAD身份验证与控制台应用程序一起使用吗? [英] Is it possible to use AzureAD authentication with a console application?
问题描述
其想法是使它像Azure Powershell一样工作,即,弹出标准Active Directory身份验证对话框,输入我的凭据,AzureAD执行其操作,然后控制台应用程序无法访问特定的Web服务.
The idea is to make it work like Azure Powershell, i.e. the standard Active Directory authentication dialog pops up, I enter my credentials, AzureAD does its thing and then the console application cann access a certain webservice.
这可能吗?不应该那么难,但是我找不到任何例子.
Is this possible? It shouldn't be that hard but I could find no example.
看一下WPF示例,我看到它在app.config中设置了一个客户端ID.这真的有必要吗?我的意思是,单页js应用程序也未在AD中注册,是吗?
Looking at the WPF example, I see it sets a client id in the app.config. Is this really necessary? I mean, a single page js app isn't registered with the AD either, is it?
推荐答案
实际上,任何客户端应用程序都应在AAD中进行注册.
In fact, any client application should be regsitered in AAD.
控制台应用程序也必须在Azure AD中注册,因此您将具有客户端ID和客户端重定向URL,但没有客户端密码.然后,下面的代码应该起作用:
Console app must be registered in Azure AD too, so you'll have client id and client redirect url, but no client secret. Then the following code should work:
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);
}
这篇关于可以将AzureAD身份验证与控制台应用程序一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!