ASP.Net Core 2如何在身份验证后触发/调用代码 [英] ASP.Net Core 2 how to trigger/call code after authentication

查看:36
本文介绍了ASP.Net Core 2如何在身份验证后触发/调用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在身份验证后立即触发一些代码?假设我想给我的应用程序每个用户一个会话限制.如何在ASP.Net Core 2.0中实现它?

更新:

澄清,我想在用户被视为经过身份验证后立即触发代码(.net身份验证中间件已确认用户声称是谁之后).我之前没有提到过中间件,因为可能有一种更简单的方法(例如事件)来触发一些代码.也许合适的问题是是否存在这样的事件/方式,或者我是否应该通过身份验证后立即附加的自定义中间件来管理它?

解决方案

最简单的方案是在MVC控制器中的任何操作中调用所需的方法.始终在身份验证后调用操作(我认为这不是您所需要的,但是您提供的信息非常差,因为您没有确切地提及身份验证后 的含义).

另一种可能性(可能是您想要的)是创建一个中间件,并将其恰好在身份验证中间件之后添加到管道中.当您将中间件投入使用时,您可以编写以完成所需的任何事情.

关于中间件的书面记录在这里:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs = aspnetcore2x

How can I trigger some code right after authentication? Let's say I want to give my application a single session constraint per user, for example. How can I implement it in ASP.Net Core 2.0?

Update:

Clarification, I want to trigger code right after a user is considered authenticated (after .net authentication middleware have validated the user is who claims to be). I have not mentioned Middleware before because there might be an easier way, like an event, to trigger some code. Maybe the proper question would be if is there such event/way, or if I should manage it through a custom Middleware attached right after Authentication?

解决方案

The simplest scenario, is calling the method you want in any action in MVC controller. Actions are always called after authentication (I think this is not what you need, but the information your provided are very poor, as you did not mentioned exactly what do you mean by right after authentication).

The other possiblity (which is probably what you want), is to create a middleware, and add it to the pipeline exactly after authentication middleware. When you have your middleware in the pipeline, you can write to do what ever you want.

Middleware writing is well documented here:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x

这篇关于ASP.Net Core 2如何在身份验证后触发/调用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆