473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get UserName

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
2 4675
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Candice | last post by:
Please somebody help! I've deleted my admin username and password which was initially set at test. Now I can't log into my website as the administrator. How do I put Username and Password back so...
11
by: Ghazan Haider | last post by:
I am posting this for the lack of a better newsgroup, and for the knowledge of people here, and because its only partially OT. We have a bunch of users with their computers at work. There are...
14
by: Brent Burkart | last post by:
I am trying to capture the Windows Authenticated username, but I want to be able to capture the login name that exists in IIS, not Windows. In order to enter my company's intranet through the...
8
by: Philip Wagenaar | last post by:
I need to send printjobs to a printqueue under diffrent usernames. The printsoftware on the queue is not very 'secure' so I can create a user on 1 system and send a printjob under it's name to the...
0
by: Sid DeLuca | last post by:
I'm modifying the WSE2 HOL sample to develop my web service. I'm able to get the client application's username token encrypted on the way out (outputTrace.webinfo), but not on receipt from the...
5
by: libra786 | last post by:
I have created a blog and have added a login box which prompts the user for login and id before posting- The username and password have been stored in the database, however when i enter the username...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
4
nathj
by: nathj | last post by:
Hi, I'm working on a registration form and one of the checks I need to perform as the form is used is on the username. I need to ensure that it is not already in use. I am getting a little...
24
by: runway27 | last post by:
i have used the following code to validate the username it is working fineif( $username == "" || !preg_match("/^+(?:_+)?$/i", $username) ) { $error.="User name cannot be blank or has special...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.