ASP MVC how to log off but remain on the same page
In asp mvc the default behaviour is to redirect the user back to a predefined route when the user logs off, ie Home/Index.
If my users are on a page of my web application that does not require authentication, is there a way for my user to log off but remain on the same page, but only do this when they are on a page or action that does not require authentication?
I can redirect to the url referrer, the problem with that is that if they are on a page that does require authentication, then they will end up being redirected to the log on page, when I would prefer in that 开发者_高级运维instance for them to be redirected to the home page.
If you need to support a page that supports both authenticated and unauthenticated users, then make it a feature not a requirement. You'll need to adjust your code on those pages to implicitly check for a logged in user, but not bail out if the user is not logged in. E.g., you could use something similar to:
if (Request.IsAuthenticated) { // etc.
Then, when you do a logout, use the referrer and redirect the user back. When they get there the page should still allow them through.
精彩评论