根据 CNIC 号码生成会话,并在 5 分钟不活动后将其过期,并将用户重定向到主页
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请允许我发布一个示例,这只是一个示例,但它对我有用。
首先,我有一个控制器主操作,它将存储会话。在您的场景中,您必须根据用户输入的内容来存储会话。
然后我创建了一个操作过滤器来拦截所有请求,过滤器应该检查传入请求是否直接到主页以及是否有会话值。所以过滤器应该是这样的:
然后在startup.cs中注册过滤器,并且不要忘记在Configure方法中添加
app.UseSession();
: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.
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:
Then register the filter in startup.cs, and pls don't forget to add
app.UseSession();
in Configure method: