By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

Get UserName

P: n/a
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
Nov 15 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)

shapper wrote:
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
Nov 15 '08 #2

P: n/a
On Nov 15, 6:40*pm, bruce barker <nos...@nospam.comwrote:
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)

shapper wrote:
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
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
Nov 15 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.