AllowAnonymous & Authorize attributes
Is it possible to decorate an action with both attributes? This would allow code to ask the question inside the action about whether the user is authenticated.
It seems it is possible with asp.net but not asp.net core: https://stackoverflow.com/questions/15770413/share-action-with-authorized-and-unauthorized-user-in-asp-net-mvc-controller
I have a use case where a user may have received a link that takes them to an action. The action will do work but ideally then redirect to a different page depending on whether the user was logged in or not.
No you can't do that with the attributes, but there's at least two other ways.
If you're using your default auth scheme (e.g. Cookies), then that authentication is processed in the UseAuthentication middleware before reaching MVC. In your action you would inspect HttpContext.User.Identity.IsAuthenticated and make your decision based on that.
If you're action is not using the default auth scheme you can always call HttpContext.AuthenticateAsync("myscheme") to get the user, if any, and then make the same decision from there.