【发布时间】:2021-06-24 21:13:32
【问题描述】:
我正在尝试使用 ASP.NET Core Web App MVC 制作 MS Teams 选项卡应用程序。该应用需要图形客户端来访问 SharePoint 资源。
我已经成功实现了微软的这个例子:https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/tab-sso/csharp
因此应正确配置应用注册。
但是通过上述教程的身份验证,我无法在“ConfigureServices”方法中注入 Microsoft Graph 客户端。
因此,我尝试将本教程:https://docs.microsoft.com/en-us/graph/tutorials/teams?tutorial-step=4 实现为 MVC 应用程序。但是现在当我尝试获取具有授权的方法时,我不断收到 401 错误。
在配置方法中,“app.UseAuthentication()”也在“app.UseAuthorization()”之前
这是我的“ConfigureServices”方法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddMicrosoftIdentityWebApiAuthentication(Configuration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(Configuration.GetSection("Graph"))
.AddInMemoryTokenCaches();
}
这是我在 index.cshtml 中的 JS
(function () {
if (microsoftTeams) {
microsoftTeams.initialize();
microsoftTeams.authentication.getAuthToken({
successCallback: (token) => {
$('<code/>', {
text: token,
style: 'word-break: break-all;'
}).appendTo('#tab-container');
fetch('/GetTest', {
method: 'get',
headers: {
"Content-Type": "application/text",
"Authorization": "Bearer " + token
}
}).then(response => {
response.text()
.then(body => {
$('#tab-container').empty();
$('<code/>', {
text: body
}).appendTo('#tab-container');
});
}).catch(error => {
console.error(error);
renderError(error);
});
},
failureCallback: (error) => {
renderError(error);
}
});
}
})();
这是我调用的“GetTest”方法
[Authorize]
[HttpGet("GetTest")]
public async Task<string> GetTest()
{
// This verifies that the access_as_user scope is
// present in the bearer token, throws if not
HttpContext.VerifyUserHasAnyAcceptedScope(apiScopes);
// To verify that the identity libraries have authenticated
// based on the token, log the user's name
_logger.LogInformation($"Authenticated user: {User.GetDisplayName()}");
try
{
// TEMPORARY
// Get a Graph token via OBO flow
var token = await _tokenAcquisition
.GetAccessTokenForUserAsync(new[]{
"User.Read",
"Sites.ReadWrite.All" });
// Log the token
_logger.LogInformation($"Access token for Graph: {token}");
return "{ \"status\": \"OK\" }";
}
catch (MicrosoftIdentityWebChallengeUserException ex)
{
_logger.LogError(ex, "Consent required");
// This exception indicates consent is required.
// Return a 403 with "consent_required" in the body
// to signal to the tab it needs to prompt for consent
HttpContext.Response.ContentType = "text/plain";
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
await HttpContext.Response.WriteAsync("consent_required");
return null;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred");
return null;
}
}
在应用程序的这种状态下,该方法现在应该返回需要同意的 403 错误,但我只收到 401 Unauthorized 错误。
我不确定是否可以使用与教程中相同的授权,因为我的项目是 MVC 项目。
【问题讨论】:
标签: c# asp.net-core-mvc microsoft-graph-api microsoft-teams