ASP.NET MVC4 Redirect to login page

前端 未结 6 1225
再見小時候
再見小時候 2020-12-09 15:53

I\'m creating a web application using ASP.NET MVC 4 and C#.

I want all users to be logged in before using application.

I\'m using ASP.NET Membership with a c

6条回答
  •  悲哀的现实
    2020-12-09 16:30

    Put [Authorize] over each action that you want only logged in users accessing. You can also do this at the controller level, making all actions within the controller secured. The latter is probably best for you, since you probably only want all of your pages disabled for guests.

    Here's what the class-level one looks like:

    [Authorize]
    public class SomethingController
    {
        //...
    }
    

    and here's an action-level one:

    public class SomethingController
    {
        [Authorize]
        public ActionResult SomeAction(Parameter someParameter)
        {
            //...   
        }
    }
    

    Another way to do it, if all or most of your pages use the same master page, is to put:

    
    

    in the master page. That way, all pages which use that master page will redirect elsewhere if the user is not logged in. This only applies if you are using the built-in authentication, though. NOTE: This option is far less secure than the first option. Use this only if site security is not a big concern

提交回复
热议问题