2 回答

TA貢獻1825條經驗 獲得超6個贊
找到解決方法:通過 REST API 獲取令牌。在這里,我可以獲得用戶令牌或客戶端令牌來訪問圖形 api:
var client = new RestClient("https://login.microsoftonline.com/" + domainname);
var request = new RestRequest("/oauth2/token", Method.POST); request.AddBody("grant_type", "client_credentials");
request.AddParameter("client_id", clientId);
request.AddParameter("client_secret", clientSecret);
request.AddParameter("Resource", "https://graph.microsoft.com");
request.AddParameter("scope", "[scopes]");
IRestResponse response = client.Execute(request);
//contains the token
var content = response.Content;

TA貢獻1796條經驗 獲得超4個贊
根據您的描述,我假設您需要一個解決方案來驗證用戶而無需任何交互。
我們可以通過一些后臺服務或守護進程獲取訪問令牌。
更多細節,我們可以參考這個文檔。
根據我的測試,我們可以嘗試以下步驟:
首先,我們應該得到管理員的同意:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
Scope = "openid profile",
ResponseType = "id_token",
TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, NameClaimType = "name" },
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = this.OnAuthenticationFailedAsync,
SecurityTokenValidated = this.OnSecurityTokenValidatedAsync
}
});
ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(Startup.clientId, string.Format(AuthorityFormat, tenantId), Startup.redirectUri,
new ClientCredential(Startup.clientSecret), null, appTokenCache.GetMsalCacheInstance());
AuthenticationResult authResult = await daemonClient.AcquireTokenForClientAsync(new[] { MSGraphScope });
然后,我們可以使用此訪問令牌來使用 Graph API。
有關更多詳細信息,我們可以查看GitHub 上的v2.0 守護程序示例。
- 2 回答
- 0 關注
- 230 瀏覽
添加回答
舉報