locked
ASP.NET MVC - BaseController - HttpContext.User.Identity.Name is null RRS feed

  • Question

  • User135423268 posted

    Good Day Everyone

    I'm trying to call the current user's name in BaseController but I'm having an error that the HttpContext.User.Identity.Name is null, but if it's on the page controller the Name is not null here's my code..

    Public Sub New()
    
    Dim getCurrUser As String = GetCurrentLogUser() 'User.Identity.Name
    
    Dim GetUserDetails As UserInfo = getWEBAPI.GetUserDetails(getCurrUser)
    
    ViewBag.GetFullName = GetUserDetails.FullName End Sub

    Thanks and regards

    Tuesday, June 25, 2019 3:35 AM

All replies

  • User753101303 posted

    Hi,

    Could it be that the controller is created before User is available (for example to avoid doing that and then see you can't find/create the controller)? Generally speaking having non initialization code inside a constructor is considered bad.

    Here I would likely start by having an extension method on User and would call that from my general layout page. If you want to keep your current approach, it would go rather in BeginInvoke for example...

    Tuesday, June 25, 2019 8:30 AM
  • User1120430333 posted

    Your  basecontroller logic should only be used after login, and it looks to me that the basecontroller should check for Autentication and redirect to the login page if user is not logged in. I can't say that your approach in using a basecontroller is an optimal approach. 

    https://www.code-sample.com/2014/08/base-controller-in-mvc-5.html

    My usage of a basecontroller for Global Exception Handling where all controllers inherit from the basecontroller  to catch all exception and any controller or locig the controller is calling. There is no try/catch anywhere in any code, becuase the base controller catches all unhandled exceptions and logs it.

    Imports System.Web.Mvc
    Imports log4net
    
    Namespace Controllers
        Public MustInherit Class BaseController
            Inherits Controller
    
            private ReadOnly _logger As ILog
    
            public sub New()
                _logger = LogManager.GetLogger(GetType(BaseController))
            End sub
    
            Protected Overrides Sub OnException(filterContext As ExceptionContext)
                MyBase.OnException(filterContext)
    
                dim appexception as AppException = New AppException(CStr(filterContext.Exception.ToString()), _logger) 
    
                Server.ClearError()
    
                RedirectToControllers("Home", "TheError")
    
            End Sub
    
            private sub RedirectToControllers(control As string, action As string)
    
                dim routeData = new RouteData()
    
                routeData.Values("controller") = control
                routeData.Values("action") = action
    
                Dim controller As IController = new HomeController(nothing) 
                controller.Execute(New RequestContext(new HttpContextWrapper(system.Web.HttpContext.Current), routeData))
    
            End sub
            
        End Class
    End Namespace

    Tuesday, June 25, 2019 9:58 AM
  • User1520731567 posted

    Hi amendoza29,

    According to your code,I can't find any useful information.

    amendoza29

    the current user's name in BaseController but I'm having an error that the HttpContext.User.Identity.Name is null, but if it's on the page controller the Name is not null

    But according to this line,I suggest you could check if the user has not logged in yet in BaseController.

    And if you want to display UserName,you could code in Layout.cshtml,for example:

     using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
        {
        @Html.AntiForgeryToken()
    
        <ul class="nav navbar-nav navbar-right">
            <li>
                @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
            </li>
            <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
        </ul>
        }

    Best Regards.

    Yuki Tao

    Wednesday, June 26, 2019 9:48 AM