2016-10-25 5 views
1

새로운 ASP.NET Identity 클래스를 사용하기 위해 MembershipProvider 및 RoleProvider를 사용하는 이전 ASP.NET Webforms 응용 프로그램을 현재 업데이트하고 있습니다. 또한, 필자는 의존성 삽입을 위해 Autofac을 사용하고 새로운 Identity 클래스와 함께 사용할 수 있기를 원한다.ASP.NET Webform, OWIN 및 ASP.NET ID로 Autofac 미들웨어 사용 이해하기

ASP.NET 신원 이제 OWIN 미들웨어에 의존하고 난 다음 Startup.cs 클래스를 추가하여 내 응용 프로그램에서 OWIN 미들웨어 설치 프로그램을 통합 할 성공적으로 수 있었다 :

internal partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     var builder = new ContainerBuilder(); 

     AutofacConfig.RegisterTypes(builder); 
     AutofacConfig.RegisterIdentityTypes(builder, app); 

     var container = builder.Build(); 
     Global.SetContainer(container); 

     app.UseAutofacMiddleware(container); 

     ConfigureAuth(app); 
    } 
} 

내가 신원 클래스에 등록을 Autofac과 같이 : 나는 그 Autofac가 작동하는지 나에게 알 수 있도록 다음과 같은 ApplicationSignInManager 클래스는 인스턴스화하고 생성자 매개 변수도 해결 로그인 페이지 내 웹폼에 탐색 할 때

internal static class AutofacConfig 
{  
    public static void RegisterIdentityTypes(ContainerBuilder builder, IAppBuilder app) 
    { 
     builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerRequest(); 
     builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest(); 
     builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest(); 
     builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest(); 
     builder.Register(c => app.GetDataProtectionProvider()).InstancePerRequest(); 
    } 
} 

내가 볼 수있는 올바르게.

public class ApplicationSignInManager : SignInManager<ApplicationUser, string> 
{ 
    public ApplicationSignInManager(
     ApplicationUserManager userManager, 
     IAuthenticationManager authenticationManager) 
     : base(userManager, authenticationManager) 
    { 
     // userManager is valid 
     // authenticationManager is valid 
    } 
} 

나는 다음과 같은 방법 페이지 로그인 내 웹폼 버튼 나의 기호를 클릭

가 호출되고 ApplicationSignInManager은 Autofac 속성 주입을 통해 인스턴스화됩니다. 그런 다음 올바른 Identity 클래스 구현을 호출합니다.

public partial class SignIn : BasePage 
{ 
    // Autofac property injection 
    public ApplicationSignInManager ApplicationSignInManager { get; set; } 

    .... 

    protected void SignInClick(object sender, EventArgs e) 
    { 
     // Validate the user password 
     var result = this.ApplicationSignInManager.PasswordSignIn(
      this.email.Text, 
      this.password.Text, 
      this.remember.Checked, 
      true); 

     .... 
    } 

그래서 모두가 잘 보이지만 뭔가, 나는 OWIN 및 Autofac 연결, 또는 요구되는 경우 표시되지 않습니다 나에게 잘 보이지 않는다. Startup.cs에서 다음 줄을 제거하면 해당 화재에 연료를 추가하십시오 ...

app.UseAutofacMiddleware(container); 

... 모든 것이 제대로 작동합니다! 어느 것이 옳을 수 없습니까?

protected void SignInClick(object sender, EventArgs e) 
    { 
     var signInManager = this.Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); 

     // Validate the user password 
     var result = signInManager.PasswordSignIn(
      this.email.Text, 
      this.password.Text, 
      this.remember.Checked, 
      true); 

     .... 
    } 

그러나 'signInManager'변수는 항상 NULL입니다 코드를 실행에 : MVC에 대한

많은 예제이합니다 (GetOwinContext() 호출을 통해) 신원 개체를 얻을 수있는 올바른 방법이다 제안하는 것 .

정말이 코드 라인이 필요합니까?

app.UseAutofacMiddleware(container); 

누구나 올바른 방향으로 나를 가리킬 수 있습니까?

답변

3

Documentation이 언급하는 간단한 시나리오에서

, app.UseAutofacMiddleware (용기); OWIN 요청 범위에 Autofac 수명을 추가하는 것은 물론 Autofac에 등록 된 미들웨어를 파이프 라인에 추가하는 작업을 모두 처리합니다.

확인하려면 source을 참조하십시오.

기본적으로 Autofac에서 OWIN 파이프 라인 (즉, 미들웨어)에 아무것도 삽입하는 경우이 줄이 필요합니다. 하지만 시나리오에서는 그렇게하는 것처럼 보이지 않으므로 그 줄을 제거해도 아무 것도 바뀌지 않습니다.

OWIN 파이프 라인에서 개체를 확인하는 경우에는 실제로이를 수행 할 필요가 없습니다. 이미 Autofac에서 개체를 확인해야합니다.그러나 SecurityStampValidator 여전히 OWIN에서 ApplicationUserManager 해결합니다, 그래서 당신은 여전히 ​​OWIN에 등록해야합니다

app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>()); 

이 뒤에 이유 this question/answer를 참조하십시오.

+0

좋아요, 그건 내게 의미가 있습니다. 그때 내가해야할 일은 app.UseAutofacMiddleware() 호출을 사용하거나 사용하지 않고 OWIN 미들웨어 언어 모듈을 검사하는 것입니다. OWIN 미들웨어 계층을 Autofac과 함께 사용하여 사이트에 필요한 언어를 기반으로 스레드 문화를 전환합니다. 나는 그것이 그 선없이 실패한 것을보아야한다. 나는 오늘 밤 시험 할 것이다! 명확하게 해 주셔서 감사합니다. – Gareth

+1

이전에 언급 한 내용과 조금 다릅니다. 그래서 나는 그 라인과 나의 미들웨어가 여전히 잘 작동한다고 해명했다. 그러나 나는 이제 Autofac과 함께 WebForms를 사용하고 Global.asax.cs 파일에 IContainerProviderAccessor를 구현했기 때문에 내 미들웨어에서 My Global.asax.cs 클래스를 통해 Autofac의 BeginLifetimeScope() 메소드에 액세스하여 내 DI를 해결하기 때문에 이해합니다. 개체. 따라서 app.UseAutofacMiddleware() 호출이 필요 없습니다. – Gareth

+0

@Gareth는 의미가 있습니다. 좋은 물건은 당신이 그것의 바닥에 도착했다! – trailmax