Microsoft Graph Api OAuth返回状态码200而不是302(不重定向到登录页面) [英] Microsoft Graph Api OAuth return status code 200 instead of 302 (Not redirect to login Page)

查看:287
本文介绍了Microsoft Graph Api OAuth返回状态码200而不是302(不重定向到登录页面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取Microsoft Graph API的令牌.这是第一个允许身份验证用户(microsoft)并获取用于调用令牌服务的代码的调用.请求已正确发送,但没有获取状态代码302,因此可以将其重定向到登录页面.我收到状态码200.

To Get Token for Microsoft Graph API. This is the first Call to let authentication user (microsoft) and get code for calling token service.Request properly sent but instead of getting status code 302 so that it can be redirected to login page. I am getting status code 200.

public async Task<string> GetBToken()
{
    string url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?"; //https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
    HttpClientHandler clientHandler = new HttpClientHandler();
    clientHandler.UseDefaultCredentials = true;
    clientHandler.AllowAutoRedirect = true;

    using(var client = new HttpClient(clientHandler))
    {
        client.BaseAddress = new Uri(url);
        // We want the response to be JSON.
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        url = url + "grant_type=authorization_code&client_id=" + appId + "resource=https://graph.microsoft.com/ &response_mode=form_post&response_type=code&redirect_uri=http://localhost/5341/Home/AddC &state=12345&scope=" + string.Join(" ", scopes1);

        var request = new HttpRequestMessage(HttpMethod.Get, url);
        var result1 = client.SendAsync(request).Result;

        result1.EnsureSuccessStatusCode();

        string jsonString = await result1.Content.ReadAsStringAsync(); // await response.Content.ReadAsStringAsync();

        return jsonString;
    }
}

推荐答案

您无法在后台执行OAUTH流程,需要将用户直接发送到https://login.microsoftonline.com/common/oauth2/v2.0/authorize?...(即在浏览器中打开该URL).

You can't execute the OAUTH flow behind the scenes, you need to send the user to https://login.microsoftonline.com/common/oauth2/v2.0/authorize?... directly (i.e. open that URL in a browser).

该过程应为:

  1. 将用户发送到https://login.microsoftonline.com/common/oauth2/v2.0/authorize?...
  2. 身份验证后,用户将返回到您指定的redirect_url.此回调将包含查询参数以及您在#3中使用的授权代码.
  3. https://login.microsoftonline.com/common/oauth2/v2.0/token?...发出POST.这将返回您在调用Microsoft Graph API时使用的访问令牌.
  1. Send user to https://login.microsoftonline.com/common/oauth2/v2.0/authorize?...
  2. After authentication, user gets returned to the redirect_url you specified. This callback will include query parameters with the Authorization Code you use in #3.
  3. Issue a POST to https://login.microsoftonline.com/common/oauth2/v2.0/token?.... This will return the Access Token you use when calling Microsoft Graph API.

这篇关于Microsoft Graph Api OAuth返回状态码200而不是302(不重定向到登录页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆