On Nov 15, 6:40*pm, bruce barker <nos...@nospam. comwrote:
it depends on usage. Profile.UserNam e 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.Cur rent.User.Ident ity.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.Cur rent.Profile.Us erName
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(HttpCont ext.Current.Pro file.UserName) as
ProfileHelper;
}
This is working. If the user is authenticated
HttpContext.Cur rent.Profile.Us erName 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.Pro file.UserName;
// I get always the id either authenticated or anonymous
string b = HttpContext.Req uest.AnonymousI D;
// I get the username if authenticated or "" if anonymous
string c = HttpContext.Use r.Identity.Name ;
// I get the user if authenticated or null if anonymous
MembershipUser u = Membership.GetU ser();
In Class:
// I get the username if authenticated and the id if anonymous
string a = HttpContext.Cur rent.Profile.Us erName;
// I get always the id either authenticated or anonymous
string b = HttpContext.Cur rent.Request.An onymousID;
// I get the username if authenticated or "" if anonymous
string c = HttpContext.Cur rent.User.Ident ity.Name;
// I get the user if authenticated or null if anonymous
MembershipUser u = Membership.GetU ser();
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