Azure Active Directory v2.0 查询 SharePoint 站点上的 Web API 集成
Azure active directory v2.0 query for Web API integration on a SharePoint site
我们有一个 SharePoint 发布站点,可以匿名访问托管在 Internet 上。根据最新要求,我们需要实现用户登录(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 端点构建一个 Web API,该 Web API 使用 OAuth 2.0 进行保护。但是,Web API 只能从具有相同应用程序 ID 的应用程序接收令牌。您无法从具有不同应用程序 ID 的客户端访问 Web API。客户端将无法请求或获取对您的 Web API 的权限。”
我们如何在 App Registration Portal 创建两个具有相同应用程序 ID 的应用程序?或者我们应该在 SharePoint 和 Web API 端使用相同的应用程序 ID?
不需要注册两个应用程序,您只需要注册一个应用程序。注册应用程序后,您可以使用下面的 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>
为了保护网络 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")),
});
}
我们有一个 SharePoint 发布站点,可以匿名访问托管在 Internet 上。根据最新要求,我们需要实现用户登录(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 端点构建一个 Web API,该 Web API 使用 OAuth 2.0 进行保护。但是,Web API 只能从具有相同应用程序 ID 的应用程序接收令牌。您无法从具有不同应用程序 ID 的客户端访问 Web API。客户端将无法请求或获取对您的 Web API 的权限。”
我们如何在 App Registration Portal 创建两个具有相同应用程序 ID 的应用程序?或者我们应该在 SharePoint 和 Web API 端使用相同的应用程序 ID?
不需要注册两个应用程序,您只需要注册一个应用程序。注册应用程序后,您可以使用下面的 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>
为了保护网络 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")),
});
}