【问题标题】:How to get the access token from azure using asp.net mvc如何使用 asp.net mvc 从 azure 获取访问令牌
【发布时间】:2020-10-26 13:52:55
【问题描述】:

我们只是想使用 asp.net MVC Web 应用程序获取 azure 的访问令牌。

我们必须使用下面的代码行来获取 azure 访问令牌。

 Dim authorityUri As String = "https://login.microsoftonline.com/common/oauth2/authorize"

 Dim authContext As AuthenticationContext = New AuthenticationContext(authorityUri)

 Dim TokenTask = Await authContext.AcquireTokenAsync(resource, ClientCredetial)

 Dim getToken = TokenTask.AccessToken()
 Return getToken

但是我们收到了对打开的 azure 登录页面的响应。你能帮我解决这个问题吗?

有没有办法使用asp.net MVC从azure获取令牌所以请提前建议。

【问题讨论】:

  • 没那么简单,there are a number of authentication flows,你需要用哪一个?而且,为什么用 c# 标记?
  • 您能否格式化代码,使其更具可读性?是授权流程显示登录页面,因为它不会在没有用户登录的情况下发出令牌。您需要使用 client_credentials 授权类型。 docs.microsoft.com/en-us/azure/active-directory/develop/…
  • 现在可读性很好。你能帮我使用asp/net MVC直接获取访问令牌吗?或者这是获取令牌的任何其他方式。请提出建议。
  • 嗨,您有机会查看我的答案吗?有更新吗?

标签: c# asp.net-mvc azure azure-functions access-token


【解决方案1】:

看来你要实现client credentials flow

权限不正确。应该是https://login.microsoftonline.com/{tenantId}

不确定为什么要添加 C# 标签,但您的代码是 VB。

所以这里有一个 C# 示例供您参考。

 string authority = "https://login.microsoftonline.com/{tenantId}";
 string clientId = "{clientId}";
 string secret = "{secret}";
 string resource = "{resource}";

 var credential = new ClientCredential(clientId, secret);
 AuthenticationContext authContext = new AuthenticationContext(authority);

 var token = authContext.AcquireTokenAsync(resource, credential).Result.AccessToken;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2018-06-18
    • 2023-04-04
    • 2018-03-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多