首页 文章

Azure活动目录v2.0查询SharePoint网站上的Web API集成

提问于
浏览
1

我们在互联网上托管了一个具有匿名访问权限的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 endpoints 构建使用OAuth 2.0保护的Web API . 但是,该Web API只能从具有相同应用程序ID的应用程序接收令牌 . 您无法从具有不同应用程序ID的客户端访问Web API . 客户端将无法请求或获取Web API的权限 . “

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

1 回答

  • 0

    无需注册两个应用程序,您只需要一个注册应用程序 . 注册应用程序后,您可以使用下面的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")),
        });
    }
    

相关问题