0
votes

_Layout.cshtml

<span>Hello @ViewBag.Username!</span>

MyController.cs

    public class MyController : Controller
    {
        public ActionResult Index()
        {
        System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
        string[] a = User.Identity.Name.Split('\\');
        System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
        ViewBag.Username = ADEntry.Properties["FullName"].Value.ToString();

        return View();
    }

    public ActionResult NextView()
    {
        System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
        string[] a = User.Identity.Name.Split('\\');
        System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
        ViewBag.Username = ADEntry.Properties["FullName"].Value.ToString();

        return View();
    }

    public ActionResult AnotherView()
    {
        System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
        string[] a = User.Identity.Name.Split('\\');
        System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
        ViewBag.Username = ADEntry.Properties["FullName"].Value.ToString();

        return View();
    }
}

How can I put this logic into one place so that I don't repeat myself? I need the User's name to be displayed throughout all my layouts.

3
Do you have a master page or shared view you could use? You can also create a Controller base class and abstract any commonalities there. Also make sure to have your views inherit from that new base calss - user1231231412

3 Answers

2
votes

Override the OnActionExecuting method in the controller and put the code to set the Username there.

  protected override void OnActionExecuting(ActionExecutingContext filterContext)
  {
     System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
     string[] a = User.Identity.Name.Split('\\');
     System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
     ViewBag.Username = ADEntry.Properties["FullName"].Value.ToString();

     base.OnActionExecuting(filterContext);
  }

Then you won't need to set the ViewBag.Username property. Any method in the controller will already have it set. If you need it across multiple controllers, put it into a base Controller class.

1
votes

You can use BaseController and inherit it to all the controls that you create, like so.

 public class BaseController : Controller
 {
       protected override void OnActionExecuting(ActionExecutingContext filterContext)
       {
            System.Security.Principal.WindowsIdentity wi =    System.Security.Principal.WindowsIdentity.GetCurrent();
      string[] a = User.Identity.Name.Split('\\');
      System.DirectoryServices.DirectoryEntry ADEntry = new   System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
     ViewBag.Username = ADEntry.Properties["FullName"].Value.ToString();

     base.OnActionExecuting(filterContext);
  }
}

public class MyController : BaseController
{

}
0
votes

You could try moving some logic into your model. Making your controller skinny and model fat.