Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check if user is logged in from the MVC5 Layout file

I have an MVC 5 Site, using a shared _Layout view. In this _Layout view i render my scripts in the bottom part, after the body.

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @*BootStrap must be loaded after JQuery UI in order to override the tooltip function*@
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/Session")

My Problem now, is that i want to include the Session Bundle in every page, except my Login pages. In other words, i want to use the Session Bundle only for pages where the user is logged in and they have an active session.

How can i check for this condition in my _Layout View and render the Script Render conditionally?

In other pages, i would add a bool field to my Model and then use an C# If construction to only render the Script part if true, but i do not have a Model in my _Layout View.

I am also using custom, very simple login methods, so i am not using the Identity Framework of MVC5.

EDIT I was suggested to use the Request object

@if (Request.IsAuthenticated) { @Render...}

This does not work since im using custom login, that does not work with the built in framework. I read up on how this field works, here How does Request.IsAuthenticated work?

The problem is still unresolved

like image 358
JesperGJensen Avatar asked Feb 01 '16 12:02

JesperGJensen


1 Answers

@if (Request.IsAuthenticated)
{
   // Render stuff for authenticated user
}
like image 116
jasonwarford Avatar answered Sep 29 '22 09:09

jasonwarford