我有一个使用ASP.NET Membership的ASP.NET MVC 3应用程序。我在生产环境中遇到奇怪的错误。一位客户抱怨他们在尝试使用Chrome登录时始终收到错误消息。它适用于IE。但是对于另一个客户端,当他们使用IE登录但在Chrome中工作时会出现相同的错误。
这是Logon的代码
[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)
请帮忙。
答案 0 :(得分:0)
我唯一能想到的就是在if语句中添加User != null
。
if (User != null && User.Identity.IsAuthenticated)
{
...
RegisteredPractice prac = (from p in db.RegisteredPractices
where p.Email == User.Identity.Name select p).FirstOrDefault();
if(prac != null) // Just to be on safe side.
{
displayName = prac.DentistFirstName + " " + prac.DentistSurname;
}
...
}
答案 1 :(得分:0)
这是修改后的代码。
public class LoginUtility
{
public static string GetDisplayForUser(IPrincipal User)
{
string displayName = "";
if (User != null)
{
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;
}
}