سؤال

I have these pages:

  1. _PageStart.cshtml
  2. Index.cshtml
  3. Login.cshtml

    .

    .

    .

n. OtherPage.cshtml

Basically, I want to restrict a user from accessing all the pages when he is not logged in, with one exception, "Login.cshtml".

In my "_PageStart.cshtml", when a user is not logged in, the page is redirected to "Login.cshtml". Since "Login.cshtml" is requested, the "_Pagestar.cshtml" will run and will detect that the user is not logged in, as result, an infinite redirect will be created.

The question now is:

  1. Is "_PageStart.cshtml" the best place to put the redirect?
  2. If the answer in question one is yes, then how not to apply PageStart on a specific page?
  3. If the answer in question one is no, then where? Should I create a helper and put it in every page?
هل كانت مفيدة؟

المحلول

Place the pages you want to protect inside of a different folder and title it something like 'Members'. In this folder place your _PageStart.cshtml with the following code:

@{ 
    WebSecurity.RequireAuthenticatedUser();
}

If you are using the standard directory structure illustrated in the Starter Site then your Login.cshtml should be in the Account directory and the WebSecurity helper will automatically redirect users back to your Login.cshtml page if they try to access your protected directory without authenticating their membership.

Jerrod

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top