我正在创建一个使用Microsoft Graph API连接到Microsoft Graph的控制台应用程序(如https://github.com/microsoftgraph/console-csharp-connect-sample所示)。一切工作正常,但我想知道是否存在一种方法可以验证用户身份(当我已经知道其用户名/密码时)而无需他们在界面上显示的“ Sing in your account”窗口中手动输入其凭据。桌面。这个想法基本上是在无人值守的情况下运行应用程序,因此用户无需在应用程序启动时输入其凭据。我找不到与此主题相关的任何信息。那有可能吗?
编辑
在跟踪@DanSilver发布的关于没有用户访问的链接之后,我尝试了该链接中建议的示例(https://github.com/Azure-Samples/active-directory-dotnet-daemon-v2)。尽管那是一个MVC应用程序,它迫使用户进行身份验证(正是我要避免的身份),但我还是设法在控制台应用程序中使用了该示例中的部分身份验证代码。通过对https://login.microsoftonline.com/myTenantId/adminconsent的请求为应用程序手动授权后, 我可以在无需用户交互的情况下连接到Graph的控制台应用程序中创建GraphServiceClient。因此,我将答案标记为有效。万一有人处于相同的情况下,则将GraphServiceclient创建为:
GraphServiceClient graphServiceClientApplication = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string clientId = "yourClientApplicationId";
string authorityFormat = "https://login.microsoftonline.com/{0}/v2.0";
string tenantId = "yourTenantId";
string msGraphScope = "https://graph.microsoft.com/.default";
string redirectUri = "msalXXXXXX://auth"; // Custom Redirect URI asigned in the Application Registration Portal in the native Application Platform
string clientSecret = "passwordGenerated";
ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(clientId, String.Format(authorityFormat, tenantId), redirectUri, new ClientCredential(clientSecret), null, new TokenCache()); …
Run Code Online (Sandbox Code Playgroud)