使用Owin OpenIdConnect中间件为.AspNet.Cookies设置Expires或Max-Age

时间:2015-12-13 15:22:45

标签: asp.net cookies owin azure-active-directory openid-connect

我正在运行this sample来创建使用AzureAD与Owin OpenIDConnect中间件连接的多租户Web应用程序。发布的.AspNet.Cookies在“我的客户端”和“我的服务器”之间进行身份验证始终是会话cookie。我想将其设置为Max-Age或到期日期。我尝试了多次修正但没有成功,例如,我尝试更改ExpireTimeSpan(请参阅下面的代码),但在我的浏览器Cookie检查器中,我仍然看到Expiration/ Max-Age: Session

另外,为什么SignOut方法使用openidconnect和cookie作为身份验证类型,而SignIn方法只使用openidconnect?

的AccountController

public void SignIn()
{
    HttpContext.GetOwinContext()
        .Authentication.Challenge(new AuthenticationProperties {RedirectUri = SettingsHelper.LoginRedirectRelativeUri},
            OpenIdConnectAuthenticationDefaults.AuthenticationType);

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

在Start.Auth.cs

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

    app.UseCookieAuthentication(new CookieAuthenticationOptions()
    {
        ExpireTimeSpan = TimeSpan.FromHours(1),

    });

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            AuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType,
            ClientId = SettingsHelper.ClientId,
            Authority = SettingsHelper.Authority,

            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;

                    ClientCredential credential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey);
                    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));
                    AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, SettingsHelper.AADGraphResourceId);

                    return Task.FromResult(0);
                },

                RedirectToIdentityProvider = (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);
                }
            }
        });
}

0 个答案:

没有答案
相关问题