Jee*_*Jsb 5 c# asp.net iis asp.net-identity
我已经用asp.net身份验证实现了asp.net mvc。
我使用了基于cookie的身份验证。重新启动IIS /停止并为我的站点启动IIS之后,当我打开网站时,用户将自动登录到系统。
用户cookie不会被清除,并且仍然对用户有效。重新启动iis后如何强制用户注销?
我使用了网站上的默认示例。 http://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
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, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
Run Code Online (Sandbox Code Playgroud)
为此,我做了一个绝招。
我们使用会话来存储动态变量和ASP.NET 身份,以便在ASP.NET MVC中进行身份验证。
如果会话无效,则使 cookie 无效并将用户导航到特定页面。
public class SessionHandler : ActionFilterAttribute
{
private ApplicationUserManager _userManager;
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.Current.GetOwinContext().Authentication;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public IIdentity UserIdentity
{
get { return System.Web.HttpContext.Current.User.Identity; }
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!string.IsNullOrWhiteSpace(UserIdentity.GetUserId()))
{
if (System.Web.HttpContext.Current.Session["Username"] == null)
{
AuthenticationManager.SignOut();
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "Index" },
{ "controller", "Home" }
});
}
}
}
}
Run Code Online (Sandbox Code Playgroud)在 Global.asax 文件中
添加以下代码
GlobalFilters.Filters.Add(new SessionHandler());
Run Code Online (Sandbox Code Playgroud)