2014-05-08 15 views
10

Chúng tôi có nhà cung cấp xác thực cookie tùy chỉnh đặt đặt cookie auth để mang tên máy chủ như .domain.com thay vì domain.com hoặc my.domain.com. Chúng tôi làm điều đó để cookie hoạt động trên tất cả các tên miền phụ và các miền. Nó đơn giản như hình dưới đây.Tùy chỉnh OWIN CookieAuthenticationProvider thất bại trên 1/lạnh khởi động

Issue

Trên nỗ lực rất FIRST sau khi ứng dụng lạnh bắt đầu, cookie VẪN mang miền my.domain.com (thông tin đăng nhập của chúng tôi là trên my.domain.com) mặc dù cài đặt nó vào .domain.com sau khi thực hiện mã SubdomainCookieAuthentication bên dưới (kiểm tra với điểm ngắt). Trong lần đăng nhập tiếp theo, tên máy chủ lưu trữ cookie là tốt.

Câu hỏi

Làm thế nào tôi có thể sửa lỗi này để nó hoạt động ngay cả trên các nỗ lực đầu tiên?

Tuỳ chỉnh cookie auth

public class SubdomainCookieAuthentication : CookieAuthenticationProvider 
{ 
    public override void ResponseSignIn(CookieResponseSignInContext context) 
    { 
     // We need to add a "." in front of the domain name to 
     // allow the cookie to be used on all sub-domains too 
     var hostname = context.Request.Uri.Host; 
     // works for www.google.com => google.com 
     // will FAIL for www.google.co.uk (gives co.uk) but doesn't apply to us 
     var dotTrimmedHostname = Regex.Replace(hostname, @"^.*(\.\S+\.\S+)", "$1"); 
     context.Options.CookieDomain = dotTrimmedHostname;    
     base.ResponseSignIn(context); 
    } 
} 

này được khởi tạo bên trong lớp khởi động Owin như sau

Class: Startup

File: App_start\Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app) 
{ 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Account/Login"), 
     Provider = new SubdomainCookieAuthentication() 
    }); 
} 
+0

có bao giờ bạn cố định này? – dampee

+0

Dường như lỗi, thời gian báo cáo vấn đề về github –

Trả lời

3

Tôi đã gặp vấn đề tương tự với Tên miền cookie không được đặt trong lần thử đầu tiên bằng phương pháp ResponseSignIn. Tôi đã có thể giải quyết vấn đề này bằng cách cập nhật thư viện Owin thành 3.x và sử dụng CookieManager mới để đặt Tên miền. Tìm thấy giải pháp này từ bài đăng này:

How is Owin able to set the Asp.Net Identity authentication cookies after the Application_EndRequest stage?

public class ChunkingCookieManagerWithSubdomains : ICookieManager 
{ 
    private readonly ChunkingCookieManager _chunkingCookieManager; 

    public ChunkingCookieManagerWithSubdomains() 
    { 
     _chunkingCookieManager = new ChunkingCookieManager(); 
    } 

    public string GetRequestCookie(IOwinContext context, string key) 
    { 
     return _chunkingCookieManager.GetRequestCookie(context, key); 
    } 

    public void AppendResponseCookie(IOwinContext context, string key, string value, CookieOptions options) 
    { 
     options.Domain = context.Request.Uri.GetHostWithoutSubDomain(); 
     _chunkingCookieManager.AppendResponseCookie(context, key, value, options); 
    } 

    public void DeleteCookie(IOwinContext context, string key, CookieOptions options) 
    { 
     options.Domain = context.Request.Uri.GetHostWithoutSubDomain(); 
     _chunkingCookieManager.DeleteCookie(context, key, options); 
    } 
} 

public static class UriExtensions 
{ 
    public static string GetHostWithoutSubDomain(this Uri url) 
    { 
     if (url.HostNameType == UriHostNameType.Dns) 
     { 
      string host = url.Host; 
      if (host.Split('.').Length > 2) 
      { 
       int lastIndex = host.LastIndexOf("."); 
       int index = host.LastIndexOf(".", lastIndex - 1); 
       return host.Substring(index + 1); 
      } 
      else 
      { 
       return host; 
      } 
     } 

     return null; 
    } 
} 

Sau đó, đăng ký nó trong Startup.Auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     ... 
     CookieManager = new ChunkingCookieManagerWithSubdomains(), 
     ... 
    } 
); 
Các vấn đề liên quan