Bic
Bic

Reputation: 3134

Membership.GetUser() C# .NET

I am trying to use either the UserName or (preferably) the user ID to determine what specific user is logged into my application. My intent, is to limit the amount of content that can be uploaded to my site based on the user. Whenever the user attempts to upload a document, I want to retrieve a count of all the documents they have currently uploaded. As a test run, I just added a label to my form to try and identify the user before writing a query:

// validate user. If they are not authenticated, do not let them upload files
if (!HttpContext.Current.User.Identity.IsAuthenticated || !HttpContext.Current.User.IsInRole("Administrator"))
{
    uploadLabel.Visible = false;
    user.Text = Membership.GetUser().UserName; // this line should output the username
    mapUpload.Visible = false;
    uploadButton.Visible = false;
}

I know the authentication logic works, because all other fields are not visible when logged in. Additionally, Membership.GetUser().UserName only has a value when the user IsAuthenticated; else, I get a null pointer exception. If the code is called, then Membership.GetUser().UserName appears to be setting the label to an empty text string.

Is there another way to get information for the current logged in user? As mentioned, my ultimate goal is be able to write a query with the information:

SELECT COUNT(DocumentID) FROM Documents WHERE UserID=@UserID

Thanks in advance for any assistance.

Bic

Upvotes: 0

Views: 1526

Answers (2)

coder
coder

Reputation: 13250

No need to use MembershipUser event to get currently logged in user we have another simple way to get currently logged in username you just define like this in your page

string userName = Page.User.Identity.Name;

Upvotes: 1

Bhushan Firake
Bhushan Firake

Reputation: 9448

Can't you replace

user.Text = Membership.GetUser().UserName;

with

user.Text= User.Identity.Name

Upvotes: 0

Related Questions