【问题标题】:Azure Active Directory Token URL Path?Azure Active Directory 令牌 URL 路径?
【发布时间】:2019-01-22 15:51:08
【问题描述】:

我有一个单页应用程序,它与我尝试使用 Azure Active Directory 保护的 Web API 进行通信。在使用具有本地帐户和 OAuth2 的本地 SQL 数据库之前,我已经这样做了,但我从未使用 Azure Active Directory 这样做过。

我在 Visual Studio 2017 中新建了一个 MVC Web API 项目,并将认证部分设置为“云 - 单一组织”并进入相应的域。 API 让我在创建过程中登录。我提供的登录信息已输入到 Web.Config 文件中。

<appSettings>
  <add key="ida:Tenant" value="domain.net" />
  <add key="ida:Audience" value="domain.net/WebAPI" />
  <add key="ida:ClientID" value="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" />
  <add key="ida:Password" value="XXXXXXXXXXXXXXXXXXXXXXXXXXX" />
 </appSettings>

然后在新的 Startup.Auth.cs 文件中我有以下内容:

public partial class Startup
{
    // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                TokenValidationParameters = new TokenValidationParameters {
                     ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                },
            });
    }
}

我没有看到可以在其中调用以启动登录的路径。在我的原始项目中,它是 /Token 路径,如下所示,来自我的原始 Startup.Auth.cs 文件。

PublicClientId = "self";
        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromHours(12),
            RefreshTokenProvider = new ApplicationRefreshTokenProvider(),
        };

在我的原始项目中,为了启动登录,我将在 Ajax 调用中使用 /Token 并传递用户名和密码。

$.ajax({
    url: url.getURL() + '/token',
    method: 'POST',
    data: {
        username: username,
        password: password,
        grant_type: 'password'
    },

如何在使用 Azure Active Directory 的新 Web API 中启动登录?我在 Startup.Auth.cs 文件中看不到 /token 路径或任何类似内容。这是我必须添加的内容,还是项目设置自动使用我在首次创建项目时输入的登录凭据?

【问题讨论】:

  • 不确定您所说的登录是什么意思,根据定义,您有一个 Web API,它是一种使用您在 Web 应用程序(代码授权流程)或 SPA(隐式流程)中获得的 Bearer 令牌的资源.此外,` ` 应该是 API 的 GUID(应用程序 ID),否则验证令牌会有点棘手。
  • 好吧,在您的 SPA 中使用 implicit grant flow to acquire both an id_token and access_token。样品here。然后使用 access_token 调用您的 API。
  • 需要使用 azure AD 进行身份验证的控制器方法必须使用 [Authorize] 属性进行修饰。它将客户端应用程序重定向到登录页面。
  • 因为它是一个 SPA,所以这个逻辑都是 JavaScript。 See this sample.
  • 登录流程应该发生在浏览器中。确保你的 Web API 清单在 Azure AD 清单中启用了隐式流。返回的 id_token 将作为 URL 片段返回给您,而不是响应正文。对于 access_tokens,请致电 this MSAL method。首先查看 App/Scr​​ipts/app.js。

标签: azure asp.net-web-api azure-active-directory


【解决方案1】:

将 Web API 绑定到 Azure Active Directory 时,没有可调用的“/token”URL 来启动登录。您必须将用户引导至以下网站:

http://login.microsoftonline.com/{domain}/oauth2/v2.0/authorize?client_id={client_id}&response_type=id_token&scope=openid&response_mode=fragment&state=12345&nonce=678910

确保输入 Azure 中列出的应用的实际域和 client_id 变量。此外,在 Azure 中,确保设置了回复 URL。登录完成后,API 将重定向回您的站点。这确实适用于 localhost。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 2018-03-24
    • 2019-08-25
    • 2018-02-17
    • 2021-08-13
    相关资源
    最近更新 更多