如何在不注册或登录Web应用程序的情况下获取访问令牌?

时间:2019-05-31 10:18:13

标签: azure asp.net-core azure-active-directory microsoft-graph azure-ad-b2c

我使用Web API上的azure Graph API创建了b2c用户,因为我必须在服务器端而不是在Web应用程序或移动应用程序之类的客户端中创建用户。我在链接(https://docs.microsoft.com/en-us/previous-versions/azure/ad/graph/api/users-operations#CreateLocalAccountUser)中做了一些步骤。我的问题是我无法获得用于访问受保护的Web API的访问令牌。如何获得访问令牌?

2 个答案:

答案 0 :(得分:1)

@Md Farid Uddin Kiron 是正确的,但您也可以在没有 AD B2C 用户的情况下获得访问令牌。

https://docs.microsoft.com/en-us/graph/auth-v2-service?view=graph-rest-1.0#4-get-an-access-token

令牌请求是这样构建的:

POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

client_id=535fb089-9ff3-47b6-9bfb-4f1264799865
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret=qWgdYAmab0YSkuL1qKv5bPX
&grant_type=client_credentials

示例 AD B2C:

https://login.microsoftonline.com/contosob2c.onmicrosoft.com/oauth2/v2.0/token

client_id 来自您的应用注册:

enter image description here

scopeApplication ID URI 来自 Expose an API 后跟 /.default。如果没有 /.default,您将收到如下错误:

<块引用>

"error": "invalid_scope", "error_description": "AADSTS1002012: 为范围提供值 https://contosob2c.onmicrosoft.com/11111111-1111-1111-1111-111111111111 无效。客户端凭据流必须具有范围值 /.default 后缀为资源标识符(应用程序 ID URI)...", "error_codes": [ 1002012 ],

enter image description here

如果您想从 Microsoft Graph 访问某些内容,请将 scope 设置为 https://graph.microsoft.com/.default

enter image description here

client_secret 来自 Certificates & secrets

完成请求:

enter image description here

答案 1 :(得分:0)

似乎您正在尝试使用ROPC身份验证协议获取访问令牌。这是示例。

代码段:

令牌类别:

 public class AccessTokenClass
        {
            public string token_type { get; set; }
            public string expires_in { get; set; }
            public string resource { get; set; }
            public string scope { get; set; }
            public string access_token { get; set; }
            public string refresh_token { get; set; }

        }

令牌方法:

private async Task<string> GetTokenByROPCFormat()
        {

            string tokenUrl = $"https://login.microsoftonline.com/YourTenantIdOrName/oauth2/token";

            var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);

            tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
            {
                ["grant_type"] = "password",
                ["client_id"] = "b603c7be-a866--e6921e61f925",
                ["client_secret"] = "Vxf1SluKbgu4PF0Nf3wE5oG",
                ["resource"] = "https://graph.microsoft.com",
                ["username"] = "kironmemb@MyTenant.onmicrosoft.com",
                ["password"] = "@Mypassword"

            });

            dynamic json;
            dynamic results;
            HttpClient client = new HttpClient();

            var tokenResponse = await client.SendAsync(tokenRequest);

            json = await tokenResponse.Content.ReadAsStringAsync();
            results = JsonConvert.DeserializeObject<AccessTokenClass>(json);
            Console.WriteLine("Your Refresh Token=>{0}", results.access_token);



            //  New Block For Accessing Data from API
            HttpClient newClient = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);
            HttpResponseMessage response = await newClient.SendAsync(request);

            string output = await response.Content.ReadAsStringAsync();
            return output;




        }
  

注意:请注意以下几点:   enter image description here

客户端凭据流:

在这种情况下,您可以实现Client credential grant flow:您可以获取此身份验证流here的完整代码段

如果您仍有任何疑问,请随时与我们分享感谢和快乐编码!