Vol*_*ker 6 .net console-application azure azure-active-directory
这个想法是让它像 Azure Powershell 一样工作,即弹出标准 Active Directory 身份验证对话框,我输入我的凭据,AzureAD 执行其操作,然后控制台应用程序可以访问某个 Web 服务。
这可能吗?这应该不难,但我找不到例子。
查看 WPF 示例,我发现它在 app.config 中设置了客户端 id。这真的有必要吗?我的意思是,单页 js 应用程序也没有在 AD 中注册,是吗?
事实上,任何客户端应用程序都应该在 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6129 次 |
| 最近记录: |