2015-04-27 13 views
5

Tôi có thiết lập trang web với bốn dịch vụ đăng nhập của bên thứ ba, Microsoft, VS, Github và Linkedin. Mọi thứ dường như hoạt động tốt, tôi có thể đăng nhập/đăng xuất, thêm/xóa tài khoản bên ngoài mà không gặp vấn đề gì.Nhận dạng ASP.net ngừng phân phát Cookie bên ngoài sau khi xóa tài khoản bên ngoài

Tuy nhiên, có vẻ như nó ngừng hoạt động. Khi tôi cố đăng nhập bằng bất kỳ dịch vụ nào của bên thứ ba, nó chỉ đưa tôi trở lại trang đăng nhập.

Nhìn vào ExternalLoginCallback có vẻ như AuthenticateResult.Identitynull và không thể nhận thông tin đăng nhập bên ngoài. Nhìn vào nó ở phía khách hàng có vẻ như họ không bao giờ có được external signin cookie.

Tôi vẫn không thể tái tạo thường xuyên lỗi này, vì vậy thật khó để xác định điều gì có thể xảy ra. Bất kỳ sự trợ giúp nào đều sẽ là tuyệt vời.

Cập nhật 1: tôi đã có thể xác định các bước để tái sản xuất:

  1. Đăng nhập vào tài khoản với hơn 1 đăng nhập liên quan đến
  2. Hủy bỏ một trong những thông tin đăng nhập
  3. Trong một mới trình duyệt hoặc phiên riêng tư, hãy thử đăng nhập bằng bất kỳ tài khoản của bên thứ ba nào và bạn sẽ được trả lại để đăng nhập mà không có cookie bên ngoài.

Sau khi gặp lỗi, nó sẽ không phát cookie cho bất kỳ phiên mới nào cho đến khi IIS được khởi động lại.

Cập nhật 2: Có vẻ như có liên quan đến việc thiết lập biến phiên.

Trên hành động removeLogin Tôi đã thêm giá trị vào phiên. Tôi không chắc tại sao nhưng khi tôi ngừng làm điều đó, tôi đã ngừng vấn đề của mình. Thời gian để tìm hiểu xem tại sao ... Cập nhật 3: Hình như vấn đề này đã được reported to the Katana Team

Cập nhật 4: Hình như người nào khác đã chạy vào vấn đề này. Stackoverflow post. Họ không cung cấp tất cả mã bạn cần để giải quyết nó, vì vậy tôi sẽ đưa nó vào đây như một câu trả lời.


Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app) { 

    // Configure the db context, user manager and signin manager to use a single instance per request 
    app.CreatePerOwinContext(appContext.Create); 
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
    app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create); 
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 

    // Enable the application to use a cookie to store information for the signed in user 
    // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
    // Configure the sign in cookie 
    app.UseCookieAuthentication(new CookieAuthenticationOptions { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     AuthenticationMode = AuthenticationMode.Active, 
     LoginPath = new PathString("/Login"), 
     LogoutPath = new PathString("/Logout"), 
     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, User, int>(
       validateInterval: TimeSpan.FromMinutes(30), 
       regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), 
       getUserIdCallback: (id) => (Int32.Parse(id.GetUserId())) 
      ) 
     } 
    }); 
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

    // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process. 
    app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5)); 

    // Enables the application to remember the second login verification factor such as phone or email. 
    // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from. 
    // This is similar to the RememberMe option when you log in. 
    app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie); 

    // Uncomment the following lines to enable logging in with third party login providers 
    app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions{ 
     ClientId = ConfigurationManager.AppSettings["MSA:Id"], 
     ClientSecret = ConfigurationManager.AppSettings["MSA:Secret"], 
     Caption = "Microsoft" 
    }); 

    app.UseVisualStudioAuthentication(new VisualStudioAuthenticationOptions(){ 
     AppId = ConfigurationManager.AppSettings["VSO:Id"], 
     AppSecret = ConfigurationManager.AppSettings["VSO:Secret"], 
     Provider = new VisualStudioAuthenticationProvider(){ 
      OnAuthenticated = (context) =>{ 
       context.Identity.AddClaim(new Claim("urn:vso:access_token", context.AccessToken, XmlSchemaString, "VisualStudio")); 
       context.Identity.AddClaim(new Claim("urn:vso:refresh_token", context.RefreshToken, XmlSchemaString, "VisualStudio")); 
       return Task.FromResult(0); 
      } 
     }, 
     Caption = "Visual Studio" 
    }); 

    app.UseGitHubAuthentication(new GitHubAuthenticationOptions{ 
     ClientId = ConfigurationManager.AppSettings["GH:Id"], 
     ClientSecret = ConfigurationManager.AppSettings["GH:Secret"], 
     Caption = "Github" 
    }); 

    app.UseLinkedInAuthentication(new LinkedInAuthenticationOptions { 
     ClientId = ConfigurationManager.AppSettings["LI:Id"], 
     ClientSecret = ConfigurationManager.AppSettings["LI:Secret"], 
     Caption = "LinkedIn" 
    }); 
} 

Trả lời

2

OWIN và asp.net xử lý cookie/session khác nhau. Nếu bạn ủy quyền với OWIN trước khi bạn khởi tạo phiên, bất kỳ ai sau khi phiên được khởi tạo sẽ không thể đăng nhập được.

Cách giải quyết: Thêm tin sau vào tệp Global.asax

// Fix for OWIN session bug 
    protected void Application_AcquireRequestState() { 
     Session["Workaround"] = 0; 
    } 
} 

dài hạn: Cách phiên OWIN và asp.net xử lý/cookie sẽ được sáp nhập trong vNext, sử dụng tác phẩm xung quanh cho đến khi đó. ..

+1

Để tránh ngoại lệ nếu phiên không có trong ngữ cảnh hiện tại, tôi khuyên bạn nên kiểm tra bối cảnh và phiên trước khi đặt giá trị HttpContext context = HttpContext.Current; if (context! = Null && context.Session! = Null) { if (Phiên ["Workaround"]! = Null) { Phiên ["Workaround"] = 0; } } –

+0

@EnricoTirotta - bạn có lỗi đánh máy không? Có nên nếu (Session ["Workaround"] == null) {Session ["Workaround"] = 0; } – RikRak

Các vấn đề liên quan