开发者

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.

开发者_JS百科How can I do same stuff in MVC 3.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜