Is there a best way to determine if the user is logged in, within the view? I can determine it in the controller and then send a viewbag to the view to check, but thought there might be a better way.
I was thinking of setting up a baseviewmodel to send but at present I don't need view models so I think it would be overkill.
You can do it in the View if you use it just to decide what to show in the view, that is, wich parts of the Voew to show to the logged user. If you need it tom do compex data retrieval tasks...then it is better to move it in the controller.
Those proprties are available to the view to allow us to enable a logout button and other UI elements that depend on the user's login status. This is a common practice.
EnenDaveyBoy
Participant
1465 Points
1146 Posts
checking if someone is logged in, within the view
Dec 03, 2012 08:37 PM|LINK
Hi
Is there a best way to determine if the user is logged in, within the view? I can determine it in the controller and then send a viewbag to the view to check, but thought there might be a better way.
I was thinking of setting up a baseviewmodel to send but at present I don't need view models so I think it would be overkill.
CPrakash82
All-Star
18284 Points
2841 Posts
Re: checking if someone is logged in, within the view
Dec 04, 2012 12:42 AM|LINK
You can use - User.Identity.IsAuthenticated within the view, its available as part of view.
ozkary
Contributor
2034 Points
303 Posts
Re: checking if someone is logged in, within the view
Dec 04, 2012 12:43 AM|LINK
Yes, you can use either one of these:
razor syntax:
@if(Request.IsAuthenticated) {}
or
@if(User.Identity.IsAuthenticated) {}
Those properties are set after the user is authenticated.
og-bit.com
EnenDaveyBoy
Participant
1465 Points
1146 Posts
Re: checking if someone is logged in, within the view
Dec 04, 2012 08:40 AM|LINK
is it good practise or is it better to do it in the controller?
francesco ab...
All-Star
20912 Points
3279 Posts
Re: checking if someone is logged in, within the view
Dec 04, 2012 10:23 AM|LINK
You can do it in the View if you use it just to decide what to show in the view, that is, wich parts of the Voew to show to the logged user. If you need it tom do compex data retrieval tasks...then it is better to move it in the controller.
Mvc Controls Toolkit | Data Moving Plug-in Videos
ozkary
Contributor
2034 Points
303 Posts
Re: checking if someone is logged in, within the view
Dec 04, 2012 04:27 PM|LINK
Those proprties are available to the view to allow us to enable a logout button and other UI elements that depend on the user's login status. This is a common practice.
og-bit.com
EnenDaveyBoy
Participant
1465 Points
1146 Posts
Re: checking if someone is logged in, within the view
Dec 05, 2012 12:15 AM|LINK
many thanks all