Get UserName

S

shapper

Hello,

I know that only authenticated users has a UserName. In Anonymous
users there is an ID.

If I am not wrong when getting a profile of a user, if the user is
authenticated the UserName is used. If it is anonymous it is used its
ID that is also stored on a cookie.

What is the correct way to get the UserName or Anonymous user ID of
the current user?

I am using:

HttpContext.Current.Profile.UserName

Is this the correct way?

Thanks,

Miguel
 
B

bruce barker

it depends on usage. Profile.UserName is the username of the profile
(should match the authenticated username if not anonymous). if you are
using the username for security, don't use profile, use:

HttpContext.Current.User.Identity.Name

-- bruce (sqlwork.com)
 
S

shapper

it depends on usage. Profile.UserName is the username of the profile
(should match the authenticated username if not anonymous). if you are
using the username for security, don't use profile, use:

        HttpContext.Current.User.Identity.Name

-- bruce (sqlwork.com)

Basically, I have a class that ihnerits from ProfileBase and creates a
custom Profile provider. To get the current user profile I have at the
moment the following:

public static ProfileHelper GetProfile() {
return Create(HttpContext.Current.Profile.UserName) as
ProfileHelper;
}

This is working. If the user is authenticated
HttpContext.Current.Profile.UserName returns the username. If it is
anonymous it returns the AnonymousID.

Would you do it differently?

I tested the following:

In MVC Controller:

// I get the username if authenticated and the id if anonymous
string a = HttpContext.Profile.UserName;
// I get always the id either authenticated or anonymous
string b = HttpContext.Request.AnonymousID;
// I get the username if authenticated or "" if anonymous
string c = HttpContext.User.Identity.Name;
// I get the user if authenticated or null if anonymous
MembershipUser u = Membership.GetUser();

In Class:

// I get the username if authenticated and the id if anonymous
string a = HttpContext.Current.Profile.UserName;
// I get always the id either authenticated or anonymous
string b = HttpContext.Current.Request.AnonymousID;
// I get the username if authenticated or "" if anonymous
string c = HttpContext.Current.User.Identity.Name;
// I get the user if authenticated or null if anonymous
MembershipUser u = Membership.GetUser();

What I am using is working ... I am just not sure if it is the best
way so I am testing other options.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top