FormsAuthentication.SetAuthCookie在异步操作中抛出NullReferenceException

问题描述:

我发现FormsAuthentication.SetAuthCookie正在抛出一个NullReferenceException - 对象引用未设置为天蓝色网站上的异步操作中的对象实例。FormsAuthentication.SetAuthCookie在异步操作中抛出NullReferenceException

我发现:

http://connect.microsoft.com/VisualStudio/feedback/details/743350/formsauthentication-setauthcookie-throws-nullreferenceexception-if-called-in-an-async-action-in-mvc4

不过我已经有

<appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 

集和我的代码工作正常,当我部署到Azure本地我只遇到问题。我正在使用Azure网站(https://www.windowsazure.com/en-us/home/scenarios/web-sites/),这是我的理解,这通常使用web.config?我也试图通过Azure的控制面板

enter image description here

加入appsetting和添加.ConfigureAwait(false);我期待已久的方法,但有没有运气。

下面的代码会抛出异常

public class TestController : Controller 
    { 
     public async Task<ActionResult> Index() 
     { 
      var httpResponse = await new HttpClient().GetAsync("http://www.google.com"); 
      FormsAuthentication.SetAuthCookie("test", true); 
      return View(); 
     } 
    } 

任何人都知道我能得到这个工作?

UPDATE:

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.] 
    System.Threading.Tasks.<>c__DisplayClass1c.<GetRethrowWithNoStackLossDelegate>b__1b(Task task) +91 
    System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +15 
    System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +77 
    System.Web.Mvc.Async.<>c__DisplayClass3f.<BeginInvokeAsynchronousActionMethod>b__3e(IAsyncResult asyncResult) +16 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +29 
    System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +59 
    System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +240 
    System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +12 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +31 
    System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +23 
    System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +128 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +26 
    System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 
    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +41 
    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55 
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +28 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 
    System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__4(IAsyncResult asyncResult) +28 
    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55 
    System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +31 
    System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 
    System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +23 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +59 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
    System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +96 

UPDATE

,我发现它的工作原理,如果手动设置cookie

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "test", DateTime.Now, DateTime.Now.AddMinutes(30), true, null, FormsAuthentication.FormsCookiePath); 
      string encTicket = FormsAuthentication.Encrypt(ticket); 
      Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); 

虽然我不想回答这个问题,但想知道为什么FormsAuthentication.SetAuthCookie正在抛出异常,以及它为什么在Azure网站上表现不同

+0

什么是异常的堆栈跟踪? – svick 2012-08-09 12:30:17

+0

您确定网站当前支持未发布的ASP.NET 4.5吗? – 2012-08-09 12:37:41

+0

@svick已添加堆栈跟踪 – Tom 2012-08-09 13:13:30

这个问题需要.NET 4.5组件不是特定于Azure的 - 方法FormsAuthentication.SetAuthCookie将抛出一个空引用异常在异步操作时的在调用FormsAuthentication.SetAuthCookie之前调用await语句。

最简单的解决方法是使用以下命令:

 Response.Cookies.Add(FormsAuthentication.GetAuthCookie("user-1", true)); 

另一种方法是给自己指定票创作:

 var ticket = new FormsAuthenticationTicket(
      2, 
      "user-1", 
      DateTime.Now, 
      DateTime.Now.AddDays(2), 
      true, 
      String.Empty, 
      "/"); 

     var encTicket = FormsAuthentication.Encrypt(ticket); 

     Response.Cookies.Add(new HttpCookie(".AUTH", encTicket)); 
+0

您能否提供一些关于为什么是这样吗?我多次遇到这个问题,但一直没有找到支持文档。 – shortstuffsushi 2016-07-01 18:57:18

目前,Windows 4.5不支持.NET 4.5 。您需要将项目降级到.NET 4.0,这也意味着您需要重写代码以不使用异步。

你可以尝试仓部署由您的应用程序

+0

应用程序使用.NET 4与Async Targeting Pack,我认为Stephen Cleary指出'UseTaskFriendlySynchronizationContext'仅在.NET 4.5中受支持,这是它在Azure上失败并且不在本地的原因 – Tom 2012-08-20 10:39:08