I’m playing around with using ASP.NET MVC3 and have a very simple site that only allows you to log in/create an account OR list your account details.
I was wanting to make the homepage .. bascially that. If u’re not logged in, then ask them to log in or create a new account. Otherwise, show them their account details. Simple.
So I wasn’t sure if I should do something like this ….
public ActionResult Index()
{
return User.Identity.IsAuthenticated ? View("Show") : View("Index");
}
That’s a pretty contrived example, but it basically describes my question.
I feel that this is not a good way.. but instead I should be redirecting to a different method .. like the real Show ? .. eg. /Show/UserFoo ????
I’m just not sure of what people are doing – a common pattern, here.
Thoughts?
You could use the
[Authorize]attribute which check if the user is authenticated and if not redirect to the login page.You could also write a custom authorization attribute if you don’t like the default behavior.