简体   繁体   English

针对 CNIC 号码生成 session 并在 5 分钟不活动后过期并将用户重定向到主页

[英]Generate session against CNIC Number and expire it after 5 minutes of inactivity and redirect the user to Homepage

I'm creating an ASP.NET Core MVC application and I want that on the Homepage the user will be required to enter his/her CNIC (Identity Card) number.我正在创建一个 ASP.NET 核心 MVC 应用程序,我希望在Homepage上要求用户输入他/她的 CNIC(身份证)号码。

After that, he'll be able to navigate to any page on the application.之后,他将能够导航到应用程序上的任何页面。 But if he/she is idle for 5 minutes or more then after 5 minutes if he/she tries to perform any action means to try to go to any other page then he/she will be redirected to "Homepage" and is required to enter his/her CNIC number again.但是,如果他/她空闲 5 分钟或更长时间,那么在 5 分钟后,如果他/她尝试执行任何操作意味着尝试 go 到任何其他页面,那么他/她将被重定向到“主页”并需要输入他/她的 CNIC 号码。

How can I implement this functionality using Sessions in the an ASP.NET Core MVC application?我如何在 ASP.NET 核心 MVC 应用程序中使用会话来实现此功能?

Thank you谢谢

Please allow me post a sample, it's just a sample but it worked for me.请允许我发布一个示例,这只是一个示例,但它对我有用。

Fisrtly, I have a controller home action which will store a session. In your scenario, you have to store the session according to what users entered.首先,我有一个 controller 主页操作,它将存储一个 session。在您的场景中,您必须根据用户输入的内容存储 session。

public IActionResult Index()
        {
            var value = HttpContext.Session.GetString("Code");
            if (value == null) {
                HttpContext.Session.SetString("Code", "001");
            }
            return View();
        }

Then I created an action filter to intercept all the requests, the filter should check if the incoming request is direct to the home page and whether there's a session value.然后我创建了一个动作过滤器来拦截所有的请求,过滤器应该检查传入的请求是否直接到主页以及是否有 session 值。 So the filter should look like this:所以过滤器应该是这样的:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace WebMvcNet3.Filters
{
    public class SessionFilter : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var value = context.HttpContext.Session.GetString("Code");
            var path = context.HttpContext.Request.Path;
            if (value == null && path != "/Home/Index") {
                context.Result = new RedirectResult("~/Home/Index");
                context.Result.ExecuteResultAsync(context);
            }
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            // Do something after the action executes.
        }
    }
}

Then register the filter in startup.cs, and pls don't forget to add app.UseSession();然后在startup.cs中注册过滤器,不要忘记添加app.UseSession(); in Configure method:在配置方法中:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSession(options => {
        //options.IdleTimeout = TimeSpan.FromMinutes(5);
        options.IdleTimeout = TimeSpan.FromSeconds(60);
    });
    services.AddControllersWithViews();
    services.AddScoped<SessionFilter>();
    services.AddControllers(config =>
    {
        config.Filters.Add<SessionFilter>();
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM