我有一个使用 ASP.NET Membership 的 ASP.NET MVC 3 应用程序。我在生产环境中遇到奇怪的错误。一位客户抱怨说,他们在尝试使用 Chrome 登录时总是会出错。它适用于 IE。然而,对于另一个客户端,当他们使用 IE 登录但在 Chrome 中工作时,他们会遇到同样的错误。
这是登录的代码
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
if (returnUrl.Contains("CorporaClaim"))
ViewBag.Layout = _corporaLayout;
else
ViewBag.Layout = _layout;
return Redirect(returnUrl);
}
else
{
ViewBag.Layout = _layout;
// redirect user according to user role
if (!string.IsNullOrEmpty(model.UserName))
return RedirectToDentalinkActionBasedOnRole(model.UserName);
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
ViewBag.Layout = _layout;
// If we got this far, something failed, redisplay form
return View(model);
}
在我的 _Layout.cshtml 中,我调用了以下方法以显示登录用户的名称:
@{
string displayName = DentalinkWeb.Utility.LoginUtility.GetDisplayForUser(User);
}
public class LoginUtility
{
public static string GetDisplayForUser(IPrincipal User)
{
string displayName = "";
if (User.Identity.IsAuthenticated)
{
if (User.IsInRole("Practice Admin"))
{
DentalinkEntities db = new DentalinkEntities();
RegisteredPractice prac = (from p in db.RegisteredPractices where p.Email == User.Identity.Name select p).FirstOrDefault();
displayName = prac.DentistFirstName + " " + prac.DentistSurname;
}
else if (User.IsInRole("Dentalink Admin"))
{
displayName = "Dentalink Admin";
}
}
return displayName;
}
}
}
我们得到这个错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at DentalinkWeb.Utility.LoginUtility.GetDisplayForUser(IPrincipal User)
at ASP._Page_Views_Shared__Layout_cshtml.Execute() in e:\web\dentalinkco\htdocs\Views\Shared\_Layout.cshtml:line 51
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer)
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
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.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
请帮忙。