是否可以将 AzureAD 身份验证与控制台应用程序一起使用?

Is it possible to use AzureAD authentication with a console application?(是否可以将 AzureAD 身份验证与控制台应用程序一起使用?)
本文介绍了是否可以将 AzureAD 身份验证与控制台应用程序一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这个想法是让它像 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 身份验证与控制台应用程序一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)(依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq))
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
Using Moq to determine if a method is called(使用 Moq 确定是否调用了方法)
Windows Phone 8.1 Camera Initialisation - Access Denied Exception(Windows Phone 8.1 相机初始化 - 访问被拒绝异常)