How can I have ASP.NET automatically redirect non-logged in Forms users to the login page?

前端 未结 4 1767
谎友^
谎友^ 2021-02-07 09:35

I have an ASP.NET website.

I want users who are not logged in to be automatically (re)directed to the login page, for example,

~/Account/Login.aspx
         


        
4条回答
  •  名媛妹妹
    2021-02-07 10:08

    I found the answer.

    Question: How do I automatically redirect non-logged in users to the login page?
    Answer: Deny anonymous users access


    Longer Explanation

    In order to automatically redirect non-logged in users to login page, you need to deny anonymous access to "all" pages. This is done in the site's web.config file:

    web.config

    
    
       
          ...
          
             
          
       
    
    

    The special ? token is used to represent anonymous users.

    This, when combined with telling Forms authentication where the "Login" page is:

    
    
       
          ...
          
             
          
          
             
          
       
    
    

    means that any any anonymous users will be automatically redirected to the login page.


    A question that seems to never have been asked before gets answered, and everybody lives.

提交回复
热议问题