ASP.Net Core 2 how to trigger/call code after authentication

若如初见. 提交于 2021-01-27 13:44:29

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/48016079/asp-net-core-2-how-to-trigger-call-code-after-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!