C#MVC和MS Graph问题
这里陷入了两难境地。
我有一个C#MVC应用程序(连接到sharepoint),我需要找到一种从Azure Active Directory检索用户的方法(Sharepoint不为这种类型的插件提供人员选择器)。
我想要实现的目标 – >一个搜索框,在按钮上单击它会在AD中搜索用户电子邮件或名称(可能是电子邮件),然后它应该返回一个包含Azure AD用户ID和显示名称的json。
我想过使用MS Graph来做到这一点,但我没有找到一个很好的教程来实现对MVC的Graph调用。 加! id就像一种不要求用户做任何事情的方式,只需单击搜索按钮(因此最好没有用户的身份validation令牌,没有图形应用登录或此类事情)。
这可能吗 ? 我甚至会在JS中这样做,因为它将是一个相当“封闭”的应用程序,但我记录图形实现的方式让我哭…(所以……是的……请不要指向MS图形实现文档,它的awfull)。
任何帮助将不胜感激,谢谢。
貘
根据您的问题和您发布的评论,我想您可能希望使用Microsoft Graph通过电子邮件获取用户ID和显示名称; 并且您希望在没有用户登录并同意应用程序的情况下执行此操作。 如果我误解了你的问题,请随时告诉我。
我最初的建议是你可以尝试在没有用户的情况下获得AccessToken。
根据这个引用,我们可以通过一些后台服务或守护进程获取AccessToken。
根据我的测试,我们可以尝试以下步骤:
1.获得管理员同意:
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 });
- 我们可以通过url从以下url获取用户:
https://graph.microsoft.com/v1.0/users/{email address}
:https://graph.microsoft.com/v1.0/users/{email address}
emailhttps://graph.microsoft.com/v1.0/users/{email address}
。 例如,https://graph.microsoft.com/v1.0/users/xxx.outlook.com
有关更多详细信息,我们可以参考GitHub上的v2.0守护程序示例 。