Note that there are some explanatory texts on larger screens.

plurals
  1. POFormsAuthentication.SetAuthCookie throwing NullReferenceException in async action
    primarykey
    data
    text
    <p>I am finding that <code>FormsAuthentication.SetAuthCookie</code> is throwing a NullReferenceException - Object reference not set to an instance of an object inside an async action on an azure website.</p> <p>I found the following:</p> <p><a href="http://connect.microsoft.com/VisualStudio/feedback/details/743350/formsauthentication-setauthcookie-throws-nullreferenceexception-if-called-in-an-async-action-in-mvc4" rel="noreferrer">http://connect.microsoft.com/VisualStudio/feedback/details/743350/formsauthentication-setauthcookie-throws-nullreferenceexception-if-called-in-an-async-action-in-mvc4</a></p> <p>However I already have</p> <pre><code> &lt;appSettings&gt; &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt; </code></pre> <p>set and my code works fine locally I only experience the problem when I deploy to azure. I am using Azure Websites (<a href="https://www.windowsazure.com/en-us/home/scenarios/web-sites/" rel="noreferrer">https://www.windowsazure.com/en-us/home/scenarios/web-sites/</a>), it was my understanding that this uses the web.config normally? I have also tried adding the appsetting through the Azure control panel</p> <p><img src="https://i.stack.imgur.com/mwbff.jpg" alt="enter image description here"></p> <p>And adding <code>.ConfigureAwait(false);</code> to my awaited method but have had no luck.</p> <p>The following code will throw the exception</p> <pre><code>public class TestController : Controller { public async Task&lt;ActionResult&gt; Index() { var httpResponse = await new HttpClient().GetAsync("http://www.google.com"); FormsAuthentication.SetAuthCookie("test", true); return View(); } } </code></pre> <p><strong>Anyone know how I can get this to work?</strong> </p> <p><strong>UPDATE:</strong> </p> <p>Stack Trace: </p> <pre><code>[NullReferenceException: Object reference not set to an instance of an object.] System.Threading.Tasks.&lt;&gt;c__DisplayClass1c.&lt;GetRethrowWithNoStackLossDelegate&gt;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.&lt;&gt;c__DisplayClass3f.&lt;BeginInvokeAsynchronousActionMethod&gt;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.&lt;&gt;c__DisplayClass39.&lt;BeginInvokeActionMethodWithFilters&gt;b__33() +59 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4f.&lt;InvokeActionMethodFilterAsynchronously&gt;b__49() +240 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass37.&lt;BeginInvokeActionMethodWithFilters&gt;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.&lt;&gt;c__DisplayClass2a.&lt;BeginInvokeAction&gt;b__20() +23 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass25.&lt;BeginInvokeAction&gt;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.&lt;&gt;c__DisplayClass1d.&lt;BeginExecuteCore&gt;b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;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.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;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.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__4(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +25 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55 System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +31 System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;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 </code></pre> <p><strong>UPDATE</strong></p> <p>I found it works if setting the Cookie manually </p> <pre><code>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)); </code></pre> <p>I'm not going to answer the question though as would like to know why <code>FormsAuthentication.SetAuthCookie</code> is throwing the exception and why it is behaving differently on Azure Websites</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload