【问题标题】:Azure active directory v2.0 query for Web API integration on a SharePoint site用于 SharePoint 站点上的 Web API 集成的 Azure Active Directory v2.0 查询
【发布时间】:2017-11-16 01:51:35
【问题描述】:

我们有一个托管在 Internet 上的 SharePoint 发布网站,可以匿名访问。根据最新要求,我们需要实现用户登录(AzureAD、Microsoft 个人和工作帐户等)。

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-flows

根据此处的文档,我们希望使用 Web API 来实现这一点,以从数据库中获取安全信息。我们正在考虑使用 MSAL.js 文件在 SharePoint 上进行用户登录和注销,在获得不记名令牌后,我们可以调用 Web API 以获取数据库中的其他数据。

独立 Web API 限制:“您可以使用 v2.0 端点构建受 OAuth 2.0 保护的 Web API。但是,该 Web API 只能从具有相同应用程序 ID 的应用程序接收令牌。您不能从具有不同应用程序 ID 的客户端访问 Web API。客户端将无法请求或获取对您的 Web API 的权限。”

我们如何在App Registration Portal 创建两个具有相同应用程序 ID 的应用程序?或者我们应该在 SharePoint 和 Web API 端使用相同的应用程序 ID?

【问题讨论】:

    标签: sharepoint oauth-2.0 azure-active-directory msal


    【解决方案1】:

    不需要注册两个应用程序,您只需要注册一个应用程序。注册应用程序后,您可以使用下面的 MSAL 库获取令牌以调用 Web API:

    <script class="pre">
        var userAgentApplication = new Msal.UserAgentApplication("e5e5f2d3-4f6a-461d-b515-efd11d50c338", null, function (errorDes, token, error, tokenType) {
            // this callback is called after loginRedirect OR acquireTokenRedirect (not used for loginPopup/aquireTokenPopup)
        })
        userAgentApplication.loginPopup(["user.read"]).then(function (token) {
            var user = userAgentApplication.getUser();
            console.log(token);
            // signin successful
        }, function (error) {
            // handle error
        });
    </script>
    

    为了保护 Web API,您可以使用相同的应用程序并参考以下代码:

    public void ConfigureAuth(IAppBuilder app)
    { 
        var tvps = new TokenValidationParameters
        {
            // The web app and the service are sharing the same clientId
            ValidAudience = "e5e5f2d3-4f6a-461d-b515-efd11d50c338",
            ValidateIssuer = false,
        };
    
        // NOTE: The usual WindowsAzureActiveDirectoryBearerAuthenticaitonMiddleware uses a
        // metadata endpoint which is not supported by the v2.0 endpoint.  Instead, this 
        // OpenIdConenctCachingSecurityTokenProvider can be used to fetch & use the OpenIdConnect
        // metadata document.
    
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
        {
            AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider("https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration")),
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多