【发布时间】:2023-04-02 12:48:01
【问题描述】:
我使用 Visual Studio 2015 创建了一个新项目,并启用了针对 Azure Active Directory 使用工作和学校帐户的身份验证。 下面是生成的配置函数的样子:
app.UseStaticFiles();
app.UseCookieAuthentication();
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = Configuration["Authentication:AzureAd:ClientId"],
ClientSecret = Configuration["Authentication:AzureAd:ClientSecret"],
Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
ResponseType = OpenIdConnectResponseType.CodeIdToken
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
这是尝试获取用户组的基本操作代码:
public async Task<IActionResult> Index()
{
var client = new HttpClient();
var uri = "https://graph.windows.net/myorganization/users/{user_id}/$links/memberOf?api-version=1.6";
var response = await client.GetAsync(uri);
if (response.Content != null)
{
ViewData["response"] = await response.Content.ReadAsStringAsync();
}
return View();
}
我需要使用或更改此代码以确保我可以获得用户组? 目前,响应是:
{
"odata.error":{
"code":"Authentication_MissingOrMalformed",
"message":{
"lang":"en",
"value":"Access Token missing or malformed."
},
"values":null
}
}
【问题讨论】:
-
嗨,Kiran,你对这个问题有什么了解吗?我有完全相同的问题。谢谢!
-
Kiran,如果你喜欢我的回答,请选择它。谢谢。
-
检查您的索引方法,您正在创建一个 HttpClient() 实例并设置 URL。但是,您从未在调用 GetAsync() 之前设置授权标头。尝试阅读有关 UseOpenIdConnectAuthentication 的事件和 OnTokenValidated 方法的信息,这应该可以帮助您获取访问令牌并可以引导您找到解决方案。
-
不确定这是否是您要查找的内容,但比使用 Graph IMO 获取角色和组要容易得多。我创建了一个使用 Azure AD 的 .Net Core 应用程序,该应用程序由现场公司 AD 提供。角色/用户可以在被推送到 Azure AD 的 AD 上进行管理,并且定义的应用角色是应用将使用的角色。 docs.microsoft.com/en-us/azure/architecture/…
标签: c# asp.net-core azure-active-directory .net-core-rc2