2012-08-09 68 views
11

我發現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

回答

15

這個問題需要.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

0

目前,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