0

我不断收到错误Key cannot be null. Parameter name: key。看起来它丢失了 authCookie,但Request.IsAuthenticated 仍然设置为 true。它快把我逼疯了。如果用户未登录,我也已经准备好处理。但错误仍然出现。

例如这里失败的地方

 @if (User.IsInRole("Admin"))
      {
        <button class="button-green white">Save</button>
      }

但是如果查看用户拥有的属性,它仍然IsAuthenticated = true显示并且名称和角色也是正确的,并且票仍然有效。

这是调用堆栈

 System.ArgumentNullException was unhandled by user code
HResult=-2147467261
Message=Key cannot be null.
Parameter name: key
Source=System
ParamName=key
StackTrace:
at System.Collections.Specialized.HybridDictionary.get_Item(Object key)
at System.Web.Security.RolePrincipal.IsInRole(String role)
at ASP._Page_Views_Admin_EditText_cshtml.Execute() in d:\SubVersion\Oline\Project1\Source\Oline.Portal\Views\Admin\EditText.cshtml:line 78
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
InnerException:

这是我的自定义 RoleProvier

public override string[] GetRolesForUser(string username)
{
  var user = (string)HttpContext.Current.Session["UserRoles"];
  var ret = new[] { user };
  return ret;
}
4

1 回答 1

0

问题的解决方案是 this sectionvar user = (string)HttpContext.Current.Session["UserRoles"];变为空。特别是我回去继续,在 Visual Studio 2012 中进行更改。感谢您的建议 :)

于 2013-02-23T23:30:38.627 回答