Note that there are some explanatory texts on larger screens.

plurals
  1. POBest practice to set/get information about whether user is logged in or not
    primarykey
    data
    text
    <p>I know similar questions have been posed before, but I could not find a duplicate that handles ASP.NET MVC (and the controller aspect) specifically.</p> <p>My question is as follows:</p> <p>In a MVC model, my understanding is that the controller should handle the use of HttpContext to determine who is logged on, if any. This is so the controller can present this information to the view, so that the view itself does not have to perform these look-ups.</p> <p>Is there any defacto standard on how to do it?</p> <p>My current setup is as follows [simplified]</p> <p>I have a <code>BaseController</code>, which all my other controllers inherit.</p> <p>In <code>BaseController</code>, I perform the following override:</p> <pre><code>protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); ViewData["IsUserLoggedIn"] = IsUserLoggedIn; // bool property checking HttpContext.User.Identity.IsAuthenticated; ViewData["CurrentUser"] = CurrentUser; // Property returning logged in user, or null. } </code></pre> <p>Then, naturally, in my Views I can check the ViewData values.</p> <p>How do <em>you</em> do it? I'd like to know if something is wrong with my setup, and if so, what's wrong? I'm not 100% comfortable with my solution, - mainly because I'm not too familiar with the cycle, and I'm uncertain whether I've placed my code at the correct place.</p> <p>I'm aware there might not be "one answer binds them all" here, I'm accepting the answer that offers most insight.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload