Sri*_*kat 6 c# authentication asp.net-mvc azure-active-directory openid-connect
我们有一个ASP.Net MVC,并且一直在使用OpenIdConnect身份验证和Azure AD作为授权。身份验证成功后,我们将“ AuthenticationTicket”有效期设置为8小时(下面我将其设置为15分钟进行测试)。这8小时是固定的,这意味着即使用户在应用程序上执行活动也不会滑动。
但是理想情况下,我们希望随着用户对系统的活跃而使到期日滑动。
尝试将“ SlidingExpiration”设置为True,即使这样做没有帮助。有关此主题的文档未作详细介绍。
那么,我们如何使用OpenIdConnect身份验证实现滑动到期?
以下是我们的启动代码。
namespace TestApp
{
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieManager = new SystemWebCookieManager(),
SlidingExpiration = true,
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
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) =>
{
context.AuthenticationTicket.Properties.ExpiresUtc = DateTime.UtcNow.AddHours(8);
context.AuthenticationTicket.Properties.AllowRefresh = true;
context.AuthenticationTicket.Properties.IsPersistent = true;
return Task.FromResult(0);
},
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Home/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
},
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望来自 ASP.net 或 Owin 团队的人能够提供更好的方法来做到这一点,但下面是我如何解决完全相同的问题。
为了让cookie认证优先并在cookie过期时返回401或重定向,需要将cookie认证模式设置为主动,将Open Id认证模式设置为被动。
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(15),
CookieSecure = CookieSecureOption.Always,
LoginPath = Microsoft.Owin.PathString.FromUriComponent("/Logout")
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive,
PostLogoutRedirectUri = postLogoutRedirectUri,
}
);
Run Code Online (Sandbox Code Playgroud)
从主动更改为被动后,我在请求授权资源时收到 401 响应,但刷新页面只会使用从 OpenID 调用中保存的令牌重新登录,该令牌在令牌过期时不会过期。为了解决这个问题,我在注销控制器(使用 cookie 选项上的 LoginPath 指定)中设置了一个操作,以将用户从 OpenID 和 Cookie 中注销
[Route("Logout")]
public ActionResult Logout()
{
HttpContext.GetOwinContext().Authentication.SignOut(
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
return View("~/Views/Index/Logout.cshtml");
}
Run Code Online (Sandbox Code Playgroud)
请记住,当 Open ID 设置为被动且 cookie 设置为主动时,如果所有请求不包含 cookie(即使它们包含令牌),则所有请求都将被重定向为未经授权。Open ID 请求必须专门调用 Open ID 处理程序才能获得授权并创建 cookie
来源:
https://coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/
归档时间: |
|
查看次数: |
1589 次 |
最近记录: |