简体   繁体   English

在多租户应用程序中的 Active Directory 用户映像中出现 401(未经授权)错误

[英]Getting 401 (Unauthorized) error in Active Directory user image in Multi tenant Application

I have implemented Multi tenant application using Azure Active Directory in Angular 4.After user logged into my application i'm able get user info.But user photo is not getting from the Active directory for that i have implemented Graph API like below snippet.我已经在 Angular 4 中使用 Azure Active Directory 实现了多租户应用程序。用户登录到我的应用程序后,我可以获取用户信息。但是用户照片没有从 Active Directory 中获取,因为我已经实现了如下片段的 Graph API。

public Task<UserDto> getPhoto(TenantDto tenantDto)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri(String.Format("https://graph.windows.net/{0}/users/{1}/thumbnailPhoto?api-version=1.6", tenantDto.tenantKey, tenantDto.email));
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tenantDto.token);
        HttpResponseMessage response = client.GetAsync("").Result;
        if (response.IsSuccessStatusCode)
        {
            return null;
            //Status status = response.Content.ReadAsAsync<Status>().Result;
            //if (status.Code == 200)
            //    InBoundResponse = JsonConvert.DeserializeObject<InBoundCallResponse>(status.Data.ToString());
            //return InBoundResponse;
        }
        else
        {
            return null;
        }
    }

Here tenantDto.token is nothing but a logged in user "token" While calling this Graph API i'm getting 401 (Unauthorized) error.这里tenantDto.token是一个登录用户“令牌” 在调用这个Graph API 时,我收到了401 (Unauthorized)错误。 I have tried all but no use.我已经尝试了所有但没有用。 I have changed Graph API setting s in Active Directory APP also like below attachment我已经更改了 Active Directory APP 中的 Graph API 设置,也如下图所示在此处输入图片说明

Also i have tried like below code it's working only for single tenant我也试过像下面的代码它只适用于单个租户

    [Route("AdUserImage"), HttpGet]
    public async Task<HttpResponseMessage> userImage()
    {
        var authContext = new AuthenticationContext("https://login.windows.net/sampletest.onmicrosoft.com/oauth2/token");
        var credential = new ClientCredential(clientID, clientSecret);
        ActiveDirectoryClient directoryClient = new ActiveDirectoryClient(serviceRoot, async () =>
        {
            var result = await authContext.AcquireTokenAsync("https://graph.windows.net/", credential);
            return result.AccessToken;
        });

        var user = await directoryClient.Users.Where(x => x.UserPrincipalName == "balu@sampletest.onmicrosoft.com").ExecuteSingleAsync();
        DataServiceStreamResponse photo = await user.ThumbnailPhoto.DownloadAsync();
        using (MemoryStream s = new MemoryStream())
        {
            photo.Stream.CopyTo(s);
            var encodedImage = Convert.ToBase64String(s.ToArray());
        }
        //string token = await HttpAppAuthenticationAsync();
        Status status = new Status("OK");
        status = new Status("Found", null, "User exists.");

        return Request.CreateResponse(HttpStatusCode.OK, status, _jsonMediaTypeFormatter);
    }

but i need to implement for Multi tenant app.但我需要为多租户应用程序实现。

Any Answer Appreciated.任何答案表示赞赏。

Thanks in Advance........!提前致谢........!

Delegate-user token:委托用户令牌:

1 .Acquire the token via the implict flow: 1 .通过隐式流程获取令牌:

https://login.microsoftonline.com/{tenant}/oauth2/authorize?response_type=token&client_id={clientId}&redirect_uri={redirect_uri}&resource=https%3A%2F%2Fgraph.windows.net&nonce={nonce}

2 .Call the Azure AD Graph 2 .调用 Azure AD Graph

GET: https://graph.windows.net/{tenant}/me/thumbnailPhoto?api-version=1.6
Content-Type: image/jpeg

Application token:应用令牌:

1 .Acquire the token via the client credentials flow 1 .通过客户端凭据流获取令牌

POST:https://login.microsoftonline.com/{tenant}/oauth2/token
grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&resource=https%3A%2F%2Fgraph.windows.net

2 .Call the Azure AD Graph 2 .调用 Azure AD Graph

GET:https://graph.windows.net/{tenant}/users/{upn}/thumbnailPhoto?api-version=1.6
Content-Type: image/jpeg

If you only to get the thumbnail photo of sign-in user for the multiple tenant, you should login-in with Azure AD first and acquire the access token for the delegate user and used that token to call Azure AD Graph REST.如果您只是获取多租户的登录用户的缩略图,则应先使用 Azure AD 登录并获取委托用户的访问令牌,并使用该令牌调用 Azure AD Graph REST。 Difference between these two kinds of token, you can refer the links below:这两种token的区别,可以参考以下链接:

Get access on behalf of a user 代表用户获取访问权限

Get access without a user 无需用户即可访问

I'm using Delegate-user token as per your explnation using below url https://login.microsoftonline.com/{tenant}/oauth2/authorize?response_type=token&client_id={clientId}&redirect_uri={redirect_uri}&resource=https%3A%2F%2Fgraph.windows.net&nonce={nonce}我正在根据您的解释使用委托用户令牌,使用以下网址https://login.microsoftonline.com/{tenant}/oauth2/authorize?response_type=token&client_id={clientId}&redirect_uri={redirect_uri}&resource=https%3A%2F%2Fgraph.windows.net&nonce={nonce}

But still not able receiving but i'm able getting 200 status but token is not return.i have implemented like below但仍然无法接收但我能够获得 200 状态但令牌没有返回。我已经实现如下

                var client = new HttpClient();
            client.BaseAddress = new Uri("https://login.microsoftonline.com/{TenantID}/oauth2/authorize?response_type=token&client_id={ClientID}&redirect_uri={ApplicationUrl}&resource=https%3A%2F%2Fgraph.windows.net&nonce=a9d7730c-79f3-4092-803a-07f346de2cdf");
            client.DefaultRequestHeaders.Accept.Clear();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));

            HttpResponseMessage response = client.GetAsync("").Result;
            if (response.IsSuccessStatusCode)
            {

            }
            else
            {
                //return null;
            }

It's not return the token.it is returning html content in success block它不是返回令牌。它在成功块中返回 html 内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在多租户应用程序中限制其他活动目录用户 - How to restrict other active directory user in multi tenant application 应用程序更新程序-不断收到401未经授权的错误 - Application Updater - Keep getting 401 Unauthorized error 在上传文件时出现401未经授权的错误 - Getting 401 Unauthorized error on file upload 从我的C#应用​​程序访问webDav文件夹时出现401未经授权的错误? - Getting 401 unauthorized error while accessing webDav folder from my C# application? 在C#窗口应用程序中SSRS报告呈现中出现错误:请求失败,HTTP状态为401:未经授权 - Getting Error in SSRS report rendering in C# window application: The request failed with HTTP status 401: Unauthorized 为什么我会收到未经授权的Yahoo OAuth 2.0错误(401)? - Why i am getting Yahoo OAuth 2.0 error (401) Unauthorized? 为什么我在 Google Calendar API 中收到 (401) 未经授权的错误 - Why am I getting a (401) Unauthorized Error in Google Calendar API ASP.NET错误从Active Directory获取用户详细信息 - ASP.NET error getting user details from Active Directory 多租户应用程序允许一个用户访问许多租户帐户? - Multi-tenant application allow one user to access many tenant accounts? 在多租户应用程序中加载特定于租户的变量 - Loading the tenant specific variables in a multi tenant application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM