I’m currently working on an ASP.NET project where I’m using ASP.NET Identity to manage user accounts. There is also an API included in my web application. My current problem is that when a client makes an API request that they are not authenticated or authorized for, they are redirected to the login/forbidden page. However, I would like such API requests to return a JSON error instead of redirecting the user to the login page. Can anyone help me with how to customize this behavior in my ASP.NET application? Is there any specific middleware or settings I need to configure to achieve this behavior? Any help or advice would be greatly appreciated.
2
Answers
In your subsequent description, I understand you
In my option I suggest you could to create a Middleware to handle API requests and web page requests:
Based on what you suggest about my solution, I’ve made improvements to my code:
When I passed Authorization the API will return the current user dataļ¼
When I Unauthorized API requests it will return a json:
And there is other method :
When I passed Authorization I could get users by CallApiResource:
When I Unauthorized: