我的ASP .NET Webforms(Web应用程序)正在使用Microsoft Graph API进行登录,并且我试图存储来自AcquireTokenByAuthorizationCode方法的访问令牌,以用于下一个对Graph API的请求。
但是这失败了,因为未创建会话并提供了空值。
以上代码正在startup.cs文件中运行。
private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification)
{
var idClient = ConfidentialClientApplicationBuilder.Create(clientId)
.WithRedirectUri(postLogoutRedirectUri)
.WithClientSecret(appKey)
.Build();
var signedInUser = new ClaimsPrincipal(notification.AuthenticationTicket.Identity);
var tokenStore = new SessionTokenStore(idClient.UserTokenCache, HttpContext.Current, signedInUser);
HttpContext.Current.Response.Cookies.Add(new HttpCookie("name","test"));
string message;
string debug;
try
{
string[] AppScopes = scopes.Split(' ');
var result = await idClient.AcquireTokenByAuthorizationCode(
AppScopes, notification.Code).ExecuteAsync();
message = "Access token retrieved.";
debug = result.AccessToken;
}
catch (MsalException ex)
{
message = "AcquireTokenByAuthorizationCodeAsync threw an exception";
debug = ex.Message;
}
}
这里有同样的问题,即使更改管道标记,也无法使其在Startup上运行。您设法找到解决方案了吗?