Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I keep getting the error Key cannot be null. Parameter name: key. It looks like it loses the authCookie, but the Request.IsAuthenticated still is set to true. It's driving my crazy. Also i'm allready handling if the user is not logged in. But the error still appears.

This is the callstack
C#
Server Error in '/' Application.

Key cannot be null.
Parameter name: key

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: Key cannot be null.
Parameter name: key

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentNullException: Key cannot be null.
Parameter name: key]
   System.Collections.Specialized.HybridDictionary.get_Item(Object key) +6693750
   System.Web.Security.RolePrincipal.IsInRole(String role) +9461152
   System.Linq.Enumerable.Any(IEnumerable`1 source, Func`2 predicate) +146
   System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +200
   System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +159
   System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +96
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +446
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +382
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034


I have this Authorize Handler. Which is to try se if the problem is the fact that im complying and changing the code, that is the problem. But i still seemes to be an problem.
C#
public class CustomeAuthorizeFilter : AuthorizeAttribute
  {

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
      if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
      {
        filterContext.Result = new HttpUnauthorizedResult();
      }
      else
      {
        filterContext.Result = new RedirectToRouteResult(new
            RouteValueDictionary(new { controller = "Authentication", action = "AccessDenied" }));
      }
    }


    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
      if (httpContext.Request.Url != null && httpContext.Request.Url.IsLoopback)
      {
        var user = (string)HttpContext.Current.Session["UserRoles"];
        if (user == OlineRoles.WebAdmin)
        {
          return true;
        }
        // It was a local request => authorize the guy
        return false;
      }

      return base.AuthorizeCore(httpContext);
    }


The web.config
XML
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
  </configSections>
  <connectionStrings>
    <add name="www_oline_dkConnectionString" connectionString="Data Source=SQL03L1;Initial Catalog=www_oline_dk;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="enableSimpleMembership" value="false"/>
    <add key="autoFormsAuthentication" value="false"/>
  </appSettings>
  <system.web>
    <customErrors mode="Off" />
    <httpRuntime />
    <sessionState timeout="3000" />
    <authentication mode="Forms"  >
      <forms loginUrl="~/Authentication/LogIn" defaultUrl="~/Home/Index"
           timeout="2900" protection="All"/>
    </authentication>

    <!--<membership defaultProvider="OlineMembershipProvider">
      <providers>
        <clear />
        <add name="OlineMembershipProvider" type="Oline.Portal.Models.Login.OlineMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>-->

    <roleManager cacheRolesInCookie="true" cookieName="OlineCookie" cookiePath="/" cookieProtection="None" cookieRequireSSL="true" cookieTimeout="60" defaultProvider="OlineRoleProvider" enabled="true">
      <providers>
        <clear />
        <add name="OlineRoleProvider" type="Oline.Portal.Models.Login.OlineRoleProvider" />
      </providers>
    </roleManager>

    <compilation debug="true" targetFramework="4.0" />
    <pages controlRenderingCompatibilityVersion="4.0">
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <httpHandlers>
      <add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
    </httpHandlers>
    <globalization culture="auto" uiCulture="auto" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
    </handlers>
  </system.webServer>
  <dotless minifyCss="false" cache="true" web="false" />
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Posted
Updated 17-Feb-13 19:55pm
v4
Comments
Sergey Alexandrovich Kryukov 17-Feb-13 16:51pm    
In what line? Or simply execute it under the debugger.
—SA
mortenstarck 17-Feb-13 17:03pm    
That's the big problem. I don't no where it fails. It doesn't jump into the VS2012 but only gives the error in the browser. Yes i'm running it as debug.
Sergey Alexandrovich Kryukov 17-Feb-13 18:38pm    
What do you mean by "running as debug"? Did you debug the situation?!
—SA
mortenstarck 18-Feb-13 3:20am    
Sorry meant. I'm offcourse using debug mode in VS2012 not release mode.
Sergey Alexandrovich Kryukov 18-Feb-13 9:50am    
There are no such "modes". Those are the names of configurations, different in how the project is built. Do you know how to debug? Not just to execute, to debug? Step-by-step, etc...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900