C# 如何获取Azure Active Directory角色或用户组
我有一个win 8应用程序,我想根据角色对ADFS用户进行身份验证。 我找到一篇文章 这是一篇很好的文章,可以将Win8应用程序与Adfs集成,并使用mvc作为广告的网络客户端C# 如何获取Azure Active Directory角色或用户组,c#,asp.net-mvc,winrt-xaml,windows-8.1,adfs,C#,Asp.net Mvc,Winrt Xaml,Windows 8.1,Adfs,我有一个win 8应用程序,我想根据角色对ADFS用户进行身份验证。 我找到一篇文章 这是一篇很好的文章,可以将Win8应用程序与Adfs集成,并使用mvc作为广告的网络客户端 AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/" + domainName); AuthenticationResult result = awai
AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/" + domainName);
AuthenticationResult result = await authenticationContext.AcquireTokenAsync(resourceAppIDUri, clientID);
if (AuthenticationStatus.Succeeded != result.Status)
{}
通过使用此代码,用户获得成功的身份验证,如果成功,则我希望针对用户组对用户进行授权。有办法吗
ClaimsPrinicipal IsInRole中有一个方法,但它总是返回false。在索赔集合中,角色或用户组没有任何内容。我在网上搜索并找到了这个链接
但它使用图形api。我想要一种更简单的方式。不管怎样,我尝试使用graph Api,但在请求时
{0}/Users'{1}'/MemberOf我得到一个priviledge异常。Graph api只能由管理员权限帐户使用。
那么我如何获取当前用户登录组呢?这篇文章是针对Azure Active Directory的。AAD中获取角色的唯一方法是通过Graph API 您想要使用ADFS。ADFS不支持图形API。ADFS以索赔的形式提供角色。这使用SAML令牌 但标题中说Azure广告是IsInRole 请编辑该问题以明确您的目标拓扑ADF/AAD
Win 8应用程序被称为本机应用程序。它们通常使用OAuth。OAuth仅在ADFS 3.0即Server 2012 R2中受支持。我通过使用Graph api找到了一个解决方案。下面是用户登录后获得的带有claimsprinicipal参数的方法。我使用上面指定的链接登录用户
private async void GetToken(ClaimsPrincipal claimsPrincipal)
{
string upn = claimsPrincipal.FindFirst(
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn").Value;
string tenantID = claimsPrincipal.FindFirst(
"http://schemas.microsoft.com/identity/claims/tenantid").Value;
string requestUrl = string.Format("https://graph.windows.net/{0}/users/{1}/memberOf?api-version=2013-04-05",
tenantID, upn);
string appPrincipalID = "152313bf-2566-4bbb-8160-06013dc45679";//This is the cliend id you get after creating web api on azure
string appKey = "XP7rvrbzOXl6n94STPgI6LTqU1fOTje4cu+Cererererer8nE=";//generate it on web app on azure
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format(
"https://login.windows.net/{0}/oauth2/token?api-version=1.0",
domainName));
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string postData = "grant_type=client_credentials";
postData += "&resource=" + HttpUtility.UrlEncode("https://graph.windows.net");
postData += "&client_id=" + HttpUtility.UrlEncode(appPrincipalID);
postData += "&client_secret=" + HttpUtility.UrlEncode(appKey);
byte[] data = encoding.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
//string authorizationHeader = string.Empty;
Models.AADJWTToken token = null;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Flush();
using (var response = request.GetResponse())
{
using (var stream1 = response.GetResponseStream())
{
using (var reader = new StreamReader(stream1))
{
string str = await reader.ReadToEndAsync();
token = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.AADJWTToken>(str);
}
}
}
}
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType, token.AccessToken);
var re = await httpClient.GetAsync(requestUrl);
var se = await re.Content.ReadAsStringAsync();
//this variable hold your result with user group in json format.
}
有人能帮忙吗?谢谢你的回复。我更新了我的问题,现在需要你的帮助。