我一直收到错误Key cannot be null. Parameter name: key
。看起来它失去了authCookie,但Request.IsAuthenticated
仍然设置为true。这让我发疯了。如果用户没有登录,我也已完成处理。但错误仍然出现。
失败的地方就是这里的例子
@if (User.IsInRole("Admin"))
{
<button class="button-green white">Save</button>
}
但是如果查看用户拥有的属性,它仍然会显示IsAuthenticated = true
,并且名称和角色也是正确的,并且票证仍然有效。
这是callstack
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;
}
答案 0 :(得分:0)
问题的解决方案是,此部分var user = (string)HttpContext.Current.Session["UserRoles"];
,它变为空。特别然后我回去和堡垒,改变Visual Studio 2012中的东西。感谢您的建议:)