我正在使用针对Azure AD的OWIN和OpenID Connect测试webproject.我正在使用此示例中的大部分代码:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect
我有一个问题,我在这个文件的第27行得到一个空例外:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect/blob/master/TodoListWebApp/Utils/NaiveSessionCache的.cs
我得到异常,因为HttpContext.Current为null.
我可以看到从BeforeAccessNotification()调用Load().
我的框架版本是4.5.2,我<httpRuntime targetFramework="4.5.2" ... >在我的web.config中.
为什么HttpContext.Current在此上下文中为null?
更新:
我从示例中得到的唯一区别是我的控制器上的ActionResult不是异步的.我在一个异步任务中调用AcquireTokenSilentAsync,该异步任务是使用标准ActionResult中的.Wait()调用的.我在CMS内工作,不允许我使用异步ActionResults.
这是OnAuthorizationCodeReceived:
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
{
var code = context.Code;
var credential = new ClientCredential(ClientId, AppKey);
var userObjectID =
context.AuthenticationTicket.Identity.FindFirst(
"http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
var authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID));
var uri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
var result = await authContext.AcquireTokenByAuthorizationCodeAsync(code, uri, credential, GraphUrl);
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
MyTest.NaiveSessionCache.Load() in C:\Workspace\MyTest\src\Website\NaiveSessionCache.cs:26
MyTest.NaiveSessionCache.BeforeAccessNotification(TokenCacheNotificationArgs …Run Code Online (Sandbox Code Playgroud)