Authentication 从MS Graph中获取具有客户端ID和机密C的组#
我是新来的微软图形。我正在开发控制台应用程序,以从Azure广告中获取群组。 我在Appr registration中使用Client secret注册了该应用程序,并在Azure门户中同意该权限。我已经为Azure广告应用程序提供了重定向URLAuthentication 从MS Graph中获取具有客户端ID和机密C的组#,authentication,microsoft-graph-api,Authentication,Microsoft Graph Api,我是新来的微软图形。我正在开发控制台应用程序,以从Azure广告中获取群组。 我在Appr registration中使用Client secret注册了该应用程序,并在Azure门户中同意该权限。我已经为Azure广告应用程序提供了重定向URL using Microsoft.Graph; using Microsoft.Graph.Auth; using Microsoft.Identity.Client; public static async Task<
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
public static async Task<string> GetGroups()
{
try
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphClient = new GraphServiceClient(authenticationProvider);
var groups = await graphClient.Groups.Request().GetAsync();
return groups.FirstOrDefault().DisplayName;
//await Task.FromResult(Finduser.Mail);
}
catch (Exception ex)
{
return Task.FromResult(string.Empty).ToString();
//throw;
}
}
使用Microsoft.Graph;
使用Microsoft.Graph.Auth;
使用Microsoft.Identity.Client;
公共静态异步任务GetGroups()
{
尝试
{
IConfidentialClientApplication-secretentialclientapplication=secretentialclientapplicationbuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authenticationProvider=新的ClientCredentialProvider(机密客户端应用程序);
var graphClient=新的GraphServiceClient(authenticationProvider);
var groups=await graphClient.groups.Request().GetAsync();
返回组.FirstOrDefault().DisplayName;
//等待Task.FromResult(Finduser.Mail);
}
捕获(例外情况除外)
{
返回Task.FromResult(string.Empty).ToString();
//投掷;
}
}
但我没有得到任何输出和错误。有人能帮我吗?
另外,我想知道是否有任何方法可以获得相同的访问令牌此示例为我提供了正确的方法
根据我的要求,AppMode.cs是合适的,运行良好
public static GraphServiceClient client;
client = AuthenticationHelper.GetAuthenticatedClientForApp();
public static GraphServiceClient GetAuthenticatedClientForApp()
{
// Create Microsoft Graph client.
try
{
graphClient = new GraphServiceClient(
"https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
var token = await GetTokenForAppAsync();
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
// This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
requestMessage.Headers.Add("SampleID", "console-csharp-snippets-sample");
}));
return graphClient;
}
catch (Exception ex)
{
Debug.WriteLine("Could not create a graph client: " + ex.Message);
}
return graphClient;
}
/// <summary>
/// Get Token for App.
/// </summary>
/// <returns>Token for app.</returns>
public static async Task<string> GetTokenForAppAsync()
{
AuthenticationResult authResult;
authResult = await IdentityAppOnlyApp.AcquireTokenForClientAsync(new string[] { "https://graph.microsoft.com/.default" });
return authResult.AccessToken;
}
IUserMemberOfCollectionWithReferencesPage userGroups = client.Users[email].MemberOf.Request().GetAsync().Result;
if (userGroups.Count == 0)
{
Console.WriteLine(" user is not a member of any groups");
}
foreach (DirectoryObject group in userGroups)
{
if (group is Group)
{
Group _group = group as Group;
Console.WriteLine(" Id: {0} UPN: {1}", _group.Id, _group.DisplayName);
}
}
公共静态图形服务客户端;
client=AuthenticationHelper.GetAuthenticatedClientForApp();
公共静态图形服务客户端GetAuthenticatedClientForApp()
{
//创建Microsoft图形客户端。
尝试
{
graphClient=新的GraphServiceClient(
"https://graph.microsoft.com/v1.0",
新的DelegateAuthenticationProvider(
异步(请求消息)=>
{
var token=await GetTokenForAppAsync();
requestMessage.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
//添加此标题是为了在Microsoft Graph服务中标识我们的示例。如果为您的项目提取此代码,请删除。
Add(“SampleID”,“console-csharp-snippets-sample”);
}));
返回图形客户端;
}
捕获(例外情况除外)
{
Debug.WriteLine(“无法创建图形客户端:“+ex.Message”);
}
返回图形客户端;
}
///
///获取应用程序的令牌。
///
///应用程序的令牌。
公共静态异步任务GetTokenForAppAsync()
{
AuthenticationResult authResult;
authResult=await IdentityAppOnlyApp.AcquireTokenForClientAsync(新字符串[]{https://graph.microsoft.com/.default" });
返回authResult.AccessToken;
}
IUserMemberOfCollectionWithReferencesPage用户组=客户端。用户[email]。MemberOf.Request().GetAsync().Result;
如果(userGroups.Count==0)
{
Console.WriteLine(“用户不是任何组的成员”);
}
foreach(用户组中的DirectoryObject组)
{
如果(组是组)
{
组_组=作为组的组;
WriteLine(“Id:{0}UPN:{1}”,_group.Id,_group.DisplayName);
}
}
很高兴您找到了它。