2016-01-26 16 views
9

Tôi đang xây dựng một ứng dụng web nhiều người thuê nhà kết nối dịch vụ Office 365 sử dụng Microsoft.Owin.Security.OpenIdConnect, Phiên bản = 3.0.0.0 và Azure Active Directory với Microsoft.IdentityModel.Clients.ActiveDirectory, Phiên bản = 2.19.0.0 theo sau this sample.Microsoft.Owin.Security.OpenIdConnect với Azure Active Directory xác thực vé đời

Ứng dụng web khách của chúng tôi (tác nhân người dùng) được xác thực cho máy chủ của chúng tôi bằng cookie asp.NET trong khi xác thực giữa máy chủ của chúng tôi và máy chủ ủy quyền (Azure AD tại đây) được thực hiện với Luồng mã ủy quyền OpenID.

Chúng tôi đặt cho cookie Asp.NET 30 ngày hết hạn trượt trong suốt cuộc đời của nó. Tuy nhiên chúng tôi vẫn có một AuthenticTicket sống ngắn từ Máy chủ Quản trị ngay cả khi đặt UseTokenLifetime = true được cho là phù hợp với tuổi thọ của hai cơ chế xác thực. Short lived authentication ticket

Vấn đề chúng tôi gặp phải là: người dùng cuối phải thường xuyên liên tục (ít hơn một giờ). Câu hỏi đặt ra là, làm cách nào để tăng/thay đổi thời gian tồn tại của vé xác thực trong phần mềm trung gian openidconnect owin này?

GHI CHÚ: Tôi cũng đã đăng a question về cách sử dụng mã thông báo làm mới với ADAL. Từ những gì chúng tôi đã hiểu, vấn đề này chỉ liên quan đến xác thực. Thời gian tồn tại của access_tokenrefresh_token là mối quan tâm ủy quyền do ứng dụng ActiveDirectory quản lý độc lập với vấn đề này. Đúng nếu tôi đã sai lầm.

Startup.Auth.cs

public partial class Startup 
{ 
    public const string CookieName = ".AspNet.MyName"; 
    public const int DayExpireCookie = 30; 

    public void ConfigureAuth(IAppBuilder app) 
    { 
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

    var cookieAuthenticationOptions = new CookieAuthenticationOptions() 
    { 
     CookieName = CookieName, 
     ExpireTimeSpan = TimeSpan.FromDays(DayExpireCookie), 
     AuthenticationType = CookieAuthenticationDefaults.AuthenticationType, 
     SlidingExpiration = true, 
    }; 

    app.UseCookieAuthentication(cookieAuthenticationOptions); 

    app.UseOpenIdConnectAuthentication(
     new OpenIdConnectAuthenticationOptions 
     { 
      ClientId = SettingsHelper.ClientId, 
      Authority = SettingsHelper.Authority, 
      ClientSecret = SettingsHelper.AppKey, 
      UseTokenLifetime = true, 
      TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters 
      { 
       ValidateIssuer = false 
      }, 

      Notifications = new OpenIdConnectAuthenticationNotifications() 
      { 
       // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away. 
       AuthorizationCodeReceived = (context) => 
       { 
        var code = context.Code; 
        string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
        string signInUserId = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; 
        AuthenticationContext authContext = new AuthenticationContext(string.Format("{0}/{1}", SettingsHelper.AuthorizationUri, tenantID), new ADALTokenCache(signInUserId)); 
        ClientCredential credential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey); 
        // Get the access token for AAD Graph. Doing this will also initialize the token cache associated with the authentication context 
        // In theory, you could acquire token for any service your application has access to here so that you can initialize the token cache 
        Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)); 
        AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code, redirectUri, credential, SettingsHelper.AADGraphResourceId); 
        return Task.FromResult(0); 
       }, 

       RedirectToIdentityProvider = (RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) => 
       { 
        string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase; 
        context.ProtocolMessage.RedirectUri = appBaseUrl + SettingsHelper.LoginRedirectRelativeUri; 
        context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl + SettingsHelper.LogoutRedirectRelativeUri; 
        return Task.FromResult(0); 
       }, 
       AuthenticationFailed = (context) => 
       { 
        context.HandleResponse(); 
        return Task.FromResult(0); 
       } 
      } 
     }); 
    } 
} 

điều khiển tài khoản

public class AccountController : Controller 
{ 

    public void SignIn() 
    { 
     var dateTimeOffset = DateTimeOffset.UtcNow; 
     var authenticationProperties = new AuthenticationProperties 
     { 
      AllowRefresh = true, 
      IssuedUtc = dateTimeOffset, 
      ExpiresUtc = dateTimeOffset.AddDays(Startup.DayExpireCookie -1), 
      RedirectUri = SettingsHelper.LoginRedirectRelativeUri, IsPersistent = true 
     }; 
     HttpContext.GetOwinContext() 
      .Authentication.Challenge(authenticationProperties,OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType); 
    } 

    public void SignOut() 
    { 
     HttpContext.GetOwinContext().Authentication.SignOut(
      new AuthenticationProperties { RedirectUri = SettingsHelper.LogoutRedirectRelativeUri, }, 
      OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType); 
    } 
} 
+0

Lưu ý UseTokenLifetime làm cho phần mềm trung gian OIDC ghi đè lên thời gian chờ của phần mềm trung gian cookie, không phải theo cách khác. – Tratcher

+0

Tuổi thọ của mã thông báo được kiểm soát bởi AAD, không phải bởi bất kỳ thứ gì bạn thực hiện trong mã của riêng bạn. http://stackoverflow.com/questions/22043128/windows-azure-active-directory-expiration-of-refreshtoken – Tratcher

+0

Thx. Tài liệu tham khảo của bạn là xử lý thời gian sống của * access_token * và * refresh_token * nhưng bây giờ, sau một giờ tôi không thể chuyển thuộc tính [Authorize] của bộ điều khiển của tôi vì vậy tôi không biết làm thế nào tôi có thể yêu cầu một mã thông báo mới. Bạn chắc chắn thời gian sống của * access_token * là thủ phạm cho phản hồi 401 unauthorize? Có vẻ như liên quan đến câu hỏi khác của tôi http://stackoverflow.com/questions/35017681/when-calling-acquiretokenbyrefreshtoken-on-the-authenticationcontext-instance-wi –

Trả lời

4

Trên thực tế, tôi cần phải thiết lập UseTokenLifetime = false. Thật vậy, UseTokenLifetime = true thay đổi vé nội bộ trong cookie Asp.NET thành thời lượng mặc định là access_token là một giờ. Các nhận xét từ @Tratcher là đúng nhưng đánh lừa tôi ... Có thời gian là access_token được kiểm soát bởi Azure AD và không có gì mà tôi có thể làm về nó. Tuy nhiên, chúng tôi đã triển khai quản lý refresh_token với ADAL.NET do đó có khả năng giữ quyền xác thực/ủy quyền với máy chủ Microsoft Identity trong hơn một giờ. Đặt UseTokenLifetTime = false và sử dụng xác thực cookie với thời gian hết hạn trượt 15 ngày giữa ứng dụng khách và máy chủ của tôi hoạt động như một sự quyến rũ ngay bây giờ.

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