Use UmbracoContext.Current.Security.CurrentUser to retrieve the current user, Name and Id are properties of the returned User model. Something like the below should work.
@{
var user = UmbracoContext.Current.Security.CurrentUser;
if (user != null) {
<p>Name: @user.ProfileData.Name</p>
<p>ID: @user.ProfileData.Id</p>
}
}
var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
if (userTicket != null)
{
var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
}
Show user id and user name in front end
Is it possible to show whether a user (not member) is logged in and if so, their id and user name in the front end, ie in a template or partial view?
Thanks
Hi Tim
Absolutely should be possible.
Use UmbracoContext.Current.Security.CurrentUser to retrieve the current user, Name and Id are properties of the returned User model. Something like the below should work.
Hmm, tried this on a template with
but user is null even though I am logged in to the back end CMS as a user on the same browser..
Do I need to add any other references to the template?
Try this in your partial
oops, no that's member details, I'm after User details!
In that case you want this then Tim
Sorry, that doesn't work
are you sure
doesn't just work with members?
Ok, try this.
Sorry, no.
I am using this in a partial view, is this correct?
Similarly, I wanted to display a picked User for a node. (v7) Inside my template I needed this (but it wasn't a partial)
string OwnerS = Umbraco.Field("testPicker").ToString(); var OwnerName = ""; if (OwnerS != "") { int OwnerInt = Int32.Parse(OwnerS); OwnerName = ApplicationContext.Services.UserService.GetByProviderKey(OwnerInt).Name; } then rendered @OwnerName
I've had success in the past using this method:
From here.
You can then do things like:
is working on a reply...