6

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

Community
  • 1
  • 1
JesperGJensen
  • 403
  • 8
  • 24

2 Answers2

11
@if (Request.IsAuthenticated)
{
   // Render stuff for authenticated user
}
jasonwarford
  • 746
  • 5
  • 11
  • What does Request.IsAuthenticated check against? Is it just a bool field i have to set serverside or does it check for something? – JesperGJensen Feb 01 '16 at 13:06
  • 1
    It's built-in, assuming you're using a standard ASP.NET authentication mechanism (Windows Auth, ASP.NET Membership, Identity, etc.). If you're not, you should be. Rolling your own auth is fool-hardy at best. – Chris Pratt Feb 01 '16 at 14:02
  • That was quick. Found this and now if I'm logged in (no one else gets to login) my delete buttons show up. thanks. – JustJohn Mar 28 '17 at 05:19
0

I found an Answer.

access session variable from layout page ASP.NET MVC3 RAZOR

I am able to access the Session object from my Layout. Using that, i can check if my custom authentication object is null. If its not null, the user is logged in

@if (Session["BrugerSession"] != null)
{
    @Scripts.Render("~/bundles/Session")
}
Community
  • 1
  • 1
JesperGJensen
  • 403
  • 8
  • 24
  • 1
    Dude, don't do your own Authentication. Unless you are a security expert and even if you are there are so many things that could go wrong. Trust the solid frameworks that are there. It's easy to use the built in stuff even for weird edge cases and get rid of that session variable. Don't program with them, they are evil and stop you scaling out a web site because the session state is stored on the local machine. If you go cloud and want to scale you have to set up a database to store your session data. ASP.NET Identity Claims can store all the data you want in that session variable. – Aran Mulholland Feb 24 '17 at 02:46
  • Not to mention you've made a stateless protocol (http) stateful (hateful :) with the use of that session variable. – Aran Mulholland Feb 24 '17 at 02:49
  • This doesn't check if the user is authenticated. It checks a variable in the session.... – Liam Apr 27 '17 at 12:44