amchoni
amchoni

Reputation: 607

Check session on every page

I'm new in mvc and i try to figured out something.

I have intranet application with login page. I put some stuff in Session and in masterpage on page init events i check

If Page.User.Identity.IsAuthenticated Then
      If Session("someThing") Is Nothing Then Me.SetupSession()
End If

This is from web forms, and this is for all page.

How can I do same stuff in MVC 3.

Upvotes: 0

Views: 2010

Answers (2)

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

you can use an Action filter to secure the action method on your controller

if you have a base controller just add the below attribute otherwise you need to add this attribute on all the controller you want to secure

[Authorize] 
public class SomeController : DefaultController
{
      public ActionResult SomeAction(){
     }
}

this attribute allow you to specify a message as well

[Authorize(Message = "Access to the blah blah function requires login. Please login or create an account")]

for more info:

http://www.asp.net/mvc/tutorials/authenticating-users-with-forms-authentication-cs

Upvotes: 3

Zruty
Zruty

Reputation: 8667

Depending on where you need this, you may write a Global filter to set up the session, or do it in the controller's Initialize() method.

Upvotes: 0

Related Questions