根据 CNIC 号码生成会话,并在 5 分钟不活动后将其过期,并将用户重定向到主页

发布于 2025-01-12 05:39:43 字数 246 浏览 0 评论 0原文

我正在创建一个 ASP.NET Core MVC 应用程序,我希望在主页上要求用户输入他/她的 CNIC(身份证)号码。

之后,他将能够导航到应用程序上的任何页面。但是,如果他/她闲置了 5 分钟或更长时间,那么 5 分钟后,如果他/她尝试执行任何操作(即尝试进入任何其他页面),那么他/她将被重定向到“主页”并需要输入再次输入他/她的 CNIC 号码。

如何在 ASP.NET Core MVC 应用程序中使用会话来实现此功能?

谢谢

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.

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.

How can I implement this functionality using Sessions in the an ASP.NET Core MVC application?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2025-01-19 05:39:43

请允许我发布一个示例,这只是一个示例,但它对我有用。

首先,我有一个控制器主操作,它将存储会话。在您的场景中,您必须根据用户输入的内容来存储会话。

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

然后我创建了一个操作过滤器来拦截所有请求,过滤器应该检查传入请求是否直接到主页以及是否有会话值。所以过滤器应该是这样的:

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.
        }
    }
}

然后在startup.cs中注册过滤器,并且不要忘记在Configure方法中添加app.UseSession();

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

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.

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. 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(); 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>();
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文