小编Tho*_*sen的帖子

TokenCache.BeforeAccess上的HttpContext.Current为null

我正在使用针对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)

openid azure owin openid-connect

6
推荐指数
1
解决办法
1464
查看次数

标签 统计

azure ×1

openid ×1

openid-connect ×1

owin ×1