2013-06-26 2 views
0

У меня есть приложение ASP.NET MVC 3, использующее членство в ASP.NET. Я получаю странную ошибку в производственной среде. Один клиент жалуется, что они всегда получают ошибку при попытке входа в систему с помощью Chrome. Он работает с IE. Однако для другого клиента они получают такую ​​же ошибку при входе в IE, но работают в Chrome.asp.net членство sometmies сбой в производстве

Это код для входа в систему

[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

Можете ли вы поделиться кодом, который вызывает метод GetDisplayForUser? – SollyM

+0

Я обновил код – user2525027

ответ

0

Единственное, что я могу придумать, это добавить User != null в выражении if.

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; 
    } 

    ... 

} 
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; 
    } 
}