Model view controller 一段时间后如何在mvc中创建自动注销页面
标题-asp.net-mvc5自动注销如何在asp.net-mvc5中的某个时间后使表单自动注销并自动重定向到登录页面您需要在Model view controller 一段时间后如何在mvc中创建自动注销页面,model-view-controller,asp.net-mvc-5,asp.net-mvc-5.1,Model View Controller,Asp.net Mvc 5,Asp.net Mvc 5.1,标题-asp.net-mvc5自动注销如何在asp.net-mvc5中的某个时间后使表单自动注销并自动重定向到登录页面您需要在登录方法上创建一个会话变量。 会话将由会话[“Userid”]=Userid创建。然后需要创建自定义属性来检查会话超时。 您需要遵循的步骤包括: 在login()中创建会话变量(Post方法) 在MVC项目中创建类文件 在该文件中复制并粘贴下面的代码 公共类SessionTimeOutAttribute:ActionFilterAttribute { 公共覆盖无效OnA
登录
方法上创建一个会话变量。
会话将由会话[“Userid”]=Userid创建代码>。然后需要创建自定义属性来检查会话超时。
您需要遵循的步骤包括:
- 在login()中创建会话变量(Post方法)
- 在MVC项目中创建类文件
- 在该文件中复制并粘贴下面的代码
公共类SessionTimeOutAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
}
- 在每个控制器上添加[SessionTimeOut]属性
[会话超时]
公共类控制器名称:控制器
{
您应该添加Statup.cs文件。
1.从新项目列表中为项目添加Statup类。
2.在ConfigureService中添加以下行
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.EnableEndpointRouting =
false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddAuthorization();
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// we do this because we trust the network
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(x =>
{
x.Cookie.Name = "WriteSomeThings";
x.Cookie.SecurePolicy = CookieSecurePolicy.Always;
x.Cookie.SameSite = SameSiteMode.Strict;
x.Cookie.HttpOnly = true;
x.Cookie.IsEssential = true;
x.SlidingExpiration = true;
x.ExpireTimeSpan = TimeSpan.FromHours(8);//For Auto Logout
x.LoginPath = "/User/LogOn";
x.LogoutPath = "/User/LogOff";
x.AccessDeniedPath = "/Home/AccessDenied";
});
}
public void配置服务(IServiceCollection服务)
{
services.AddMvc(options=>options.EnableEndpointRouting=
错误)。设置兼容版本(兼容版本.Version\u 3\u 0);
services.AddAuthorization();
配置(选项=>
{
options.ForwardedHeaders=ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
//我们这样做是因为我们信任网络
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(x=>
{
x、 Cookie.Name=“WriteMethings”;
x、 Cookie.SecurePolicy=CookieSecurePolicy.Always;
x、 Cookie.SameSite=SameSiteMode.Strict;
x、 Cookie.HttpOnly=true;
x、 Cookie.IsEssential=true;
x、 slidengexpiration=true;
x、 ExpireTimeSpan=TimeSpan.FromHours(8);//用于自动注销
x、 LoginPath=“/User/LogOn”;
x、 LogoutPath=“/User/LogOff”;
x、 AccessDeniedPath=“/Home/AccessDenied”;
});
}
x、 ExpireTimeSpan=TimeSpan.FromHours(8)=>此行允许我们在8小时后自动注销。如果需要完全用户管理,请检查此视频
使用ASP.NET MVC 5进行基于角色的用户管理的软件开发模板。免费试用它您必须创建一个会话来存储用户id的值,并且需要编写代码在会话过期后执行。是的,它有效
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.EnableEndpointRouting =
false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddAuthorization();
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// we do this because we trust the network
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(x =>
{
x.Cookie.Name = "WriteSomeThings";
x.Cookie.SecurePolicy = CookieSecurePolicy.Always;
x.Cookie.SameSite = SameSiteMode.Strict;
x.Cookie.HttpOnly = true;
x.Cookie.IsEssential = true;
x.SlidingExpiration = true;
x.ExpireTimeSpan = TimeSpan.FromHours(8);//For Auto Logout
x.LoginPath = "/User/LogOn";
x.LogoutPath = "/User/LogOff";
x.AccessDeniedPath = "/Home/AccessDenied";
});
}