2014-11-22 4 views
3

가상 디렉터리에 Owin Identity 응용 프로그램과 다른 응용 프로그램이 설정되어 있습니다. 가상 응용 프로그램은 전통적인 양식 인증을 사용하여 설정되며 두 Web.config는 모두 <machineKey>으로 설정됩니다. ID 앱을 사용하여 로그인 할 수 있으며 결과 쿠키를 볼 수 있습니다. 그러나 가상 응용 프로그램에 액세스하려고하면 인증되지 않았다고 말합니다. 폼 인증에서 ASP.Net ID 2 쿠키 사용

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
    LoginPath = new PathString("/login.aspx"), 
    Provider = new CookieAuthenticationProvider 
    { 
    // Enables the application to validate the security stamp when the user logs in. 
    // This is a security feature which is used when you change a password or add an external login to your account. 
    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
     validateInterval: TimeSpan.FromMinutes(30), 
     regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
    } 
}); 

그리고 가상 응용 프로그램에서

, 내가로 설정 권한은 다음과 같습니다 : 가상 응용 프로그램을 얻을

<authorization> 
     <deny users="?" /> 
</authorization> 

모든 포인터를 식별 응용 프로그램에서

, 나는 다음과 같은 설정을 가지고 Identity에 의해 설정된 쿠키를 인식합니까?

답변

12

쿠키에 인증 티켓이 들어 있습니다. 이 티켓의 형식은 쿠키 인증 미들웨어와 폼 인증에 따라 다릅니다. FAM이 쿠키 인증 미들웨어에 의해 생성 된 쿠키를 읽게하는 것은 불가능합니다. 즉, 쿠키 인증 미들웨어에서 만든 쿠키를 읽으려면 FAM과 비슷한 자체 HTTP 모듈을 작성할 수 있습니다.

public class MyHttpModule : IHttpModule 
{ 
    public void Init(HttpApplication context) 
    { 
     context.AuthenticateRequest += OnApplicationAuthenticateRequest; 
    } 
    private static void OnApplicationAuthenticateRequest(object sender, EventArgs e) 
    { 
     var request = HttpContext.Current.Request; 
     var cookie = request.Cookies.Get(".AspNet.ApplicationCookie"); 
     var ticket = cookie.Value; 
     ticket = ticket.Replace('-', '+').Replace('_', '/'); 

     var padding = 3 - ((ticket.Length + 3) % 4); 
     if (padding != 0) 
      ticket = ticket + new string('=', padding); 

     var bytes = Convert.FromBase64String(ticket); 

     bytes = System.Web.Security.MachineKey.Unprotect(bytes, 
      "Microsoft.Owin.Security.Cookies.CookieAuthenticationMiddleware", 
       "ApplicationCookie", "v1"); 

     using (var memory = new MemoryStream(bytes)) 
     { 
      using (var compression = new GZipStream(memory, 
               CompressionMode.Decompress)) 
      { 
       using (var reader = new BinaryReader(compression)) 
       { 
        reader.ReadInt32(); 
        string authenticationType = reader.ReadString(); 
        reader.ReadString(); 
        reader.ReadString(); 

        int count = reader.ReadInt32(); 

        var claims = new Claim[count]; 
        for (int index = 0; index != count; ++index) 
        { 
         string type = reader.ReadString(); 
         type = type == "\0" ? ClaimTypes.Name : type; 

         string value = reader.ReadString(); 

         string valueType = reader.ReadString(); 
         valueType = valueType == "\0" ? 
             "http://www.w3.org/2001/XMLSchema#string" : 
             valueType; 

         string issuer = reader.ReadString(); 
         issuer = issuer == "\0" ? "LOCAL AUTHORITY" : issuer; 

         string originalIssuer = reader.ReadString(); 
         originalIssuer = originalIssuer == "\0" ? 
                issuer : originalIssuer; 

         claims[index] = new Claim(type, value, 
               valueType, issuer, originalIssuer); 
        } 

        var identity = new ClaimsIdentity(claims, authenticationType, 
                ClaimTypes.Name, ClaimTypes.Role); 

        var principal = new ClaimsPrincipal(identity); 

        System.Threading.Thread.CurrentPrincipal = principal; 
        HttpContext.Current.User = principal; 
       } 
      } 
     } 
    } 


    public void Dispose() { } 
} 

내가하는 일에 대한 설명은 제 블로그 항목으로 이동하십시오.

http://lbadri.wordpress.com/2014/11/23/reading-katana-cookie-authentication-middlewares-cookie-from-formsauthenticationmodule/

여기에 설명하기가 너무 큽니다.

+0

Badri,이 솔루션은 완벽하게 작동합니다. 당신의 대답에 대해 정말 고마워요. – ern

관련 문제