2015-01-05 2 views
2

현재 WebForms/MVP 응용 프로그램의 일부 구성 요소를 MVC로 마이그레이션 중입니다. 지금까지는 승인을 제외하고는 모든 것이 작동합니다. 아무리 로그인 페이지의 MVC 버전으로 탐색 할 때, 나는 Web.config에 설정되어있는 영문 페이지로 리디렉션 무엇 :혼합 된 MVC/WebForms 웹 응용 프로그램에서 인증 구성

<authentication mode="Forms"> 
     <forms name=".MyWebSite" enableCrossAppRedirects="true" loginUrl="Login.aspx" timeout="60" path="/" defaultUrl="~/Pages/Landing.aspx"></forms> 
    </authentication> 

내가 AllowAnonymous를 사용하여 시도했지만 웹 양식이 config (설정) 것으로 나타납니다 우선 순위가 높습니다. 여기 내 로그인 컨트롤러 :

[RouteArea("User", AreaPrefix = "")] 
public class AuthenticationController : Controller { 
    [Route("Login")] 
    [AllowAnonymous] 
    public ActionResult Login() { 
     return View(); 
    } 
} 

그리고 내 디렉토리 구조는 다음과 같습니다

<location path="Error"> 
    <system.web> 
     <authorization> 
     <allow users="*" /> 
     </authorization> 
    </system.web> 
    </location> 
: 내 Web.config의에서

> Web Project 
    > Areas 
     > User 
      > Controllers 
       > AuthController 
      > Views 
       > Login.cshtml 

, 나는 오류 페이지에 대한 익명 액세스를 허용하려면 다음을 참조

그러나 Areas 경로의 복제가 작동하지 않습니다 (아마도 aspx 페이지가있는 것처럼 실제로 cshtml 파일이 없기 때문일 수 있습니다).

이제 (aspx 버전의 로그인을 통해) 로그인하고 사용자가 인증되면 MVC 구현에 액세스 할 수 있습니다. 라우팅 및 렌더링이 훌륭하게 작동합니다. 인증되지 않은 사용자가 도전 과제 인 것처럼 보이는 aspx 구현으로 리디렉션하지 않고 MVC 페이지에 액세스 할 수있게하는 것입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

<location path="Login"> 
    <system.web> 
     <authorization> 
     <allow users="?" /> 
     </authorization> 
    </system.web> 
    </location> 
:이 같은 "로그인"을 지정할 수 있음을 의미

protected void Application_BeginRequest(object sender, EventArgs e) { 
     // lots of existing web.config controls for which webforms folders can be accessed 
     // read the config and skip checks for pages that authorise anon users by having 
     // <allow users="?" /> as the top rule. 
     // https://stackoverflow.com/questions/4616524/turning-off-asp-net-webforms-authentication-for-one-sub-directory 

     // check local config 
     var localAuthSection = ConfigurationManager.GetSection("system.web/authorization") as AuthorizationSection; 

     // this assumes that the first rule will be <allow users="?" /> 
     var localRule = localAuthSection.Rules[0]; 
     if (localRule.Action == AuthorizationRuleAction.Allow && localRule.Users.Contains("?")) { 
      // then skip the rest 
      return; 
     } 

     // get the web.config and check locations 
     var conf = WebConfigurationManager.OpenWebConfiguration("~"); 
     foreach (ConfigurationLocation loc in conf.Locations) { 
      // find whether we're in a location with overridden config 

      // get page name 
      var currentPath = Path.GetFileName(this.Request.Path); 
      if (currentPath.Equals(loc.Path, StringComparison.OrdinalIgnoreCase)) { 
       // get the location's config 
       var locConf = loc.OpenConfiguration(); 
       var authSection = locConf.GetSection("system.web/authorization") as AuthorizationSection; 
       if (authSection != null) { 
        // this assumes that the first rule will be <allow users="?" /> 
        var rule = authSection.Rules[0]; 
        if (rule.Action == AuthorizationRuleAction.Allow && rule.Users.Contains("?")) { 
         // then skip the rest 
         return; 
        } 
       } 
      } 
     } 
    } 

:

편집 정말 해키 부분적인 해결책 내가 찾은 (Turning off ASP.Net WebForms authentication for one sub-directory 기준)은 다음과 같다

하지만 관련 파일/CSS에 대한 규칙을 추가하지 않으면 관련 CSS/JS가 모두 렌더링되지 않습니다. 이있어보다 세련되게 해결할 수 있습니다.

+0

나는 혼합 된 환경을 가지고 있으며, 둘 다 오래된 aspx 로그인 페이지를 사용합니다. auth 플래그는 사용자가 인증 된 방법에 관계없이 동일합니다. aspx 및 mvc 로그인 페이지를 둘 다 사용해야 할 이유가 있습니까? – tintyethan

+0

@tintyethan - 기술적으로 불가능한 경우가 아니라면 불행히도 MVC로 마이그레이션하는 첫 번째 페이지로 선택되었습니다. 결국 ASPX 구현을 중단하지만 ASPX 인증은 그대로 유지해야합니다. – SB2055

답변

1

나는 해결책이라고 생각되는 것을 발견했습니다. 내 Web.config의, 나는 내 MVC 페이지에 loginUrl을 설정

<authentication mode="Forms"> 
     <forms name=".MyWebSite" enableCrossAppRedirects="true" loginUrl="Login" timeout="60" path="/" defaultUrl="~/Pages/Landing.aspx"></forms> 
    </authentication> 

그때 내가 aspx 페이지로 리디렉션 할 때, HttpContext.CurrentUser가 정의되도록 내 authController 내에서 쿠키를 설정해야 로그인 한 사용자 :

FormsAuthentication.SetAuthCookie(model.Username, true); 

이것이 실제로 올바른 방법인지는 모르지만 지금까지는 제대로 작동하는 것 같습니다. 누구나 피드백이있을 경우를 대비하여이 정보를 공개하겠습니다.

0

"mvc music store"라는 Microsoft의 자습서를 살펴보십시오. 7 부에서는 인증 및 권한 부여 구성에 대해 설명합니다. 5-10 페이지의 읽기와 이제는 기본 개념입니다.

관련 문제