R.S
R.S

Reputation: 219

How to access user identity in SQL query

In C# 3.0 ASP.NET I'm composing a CMS and I want to compose a SQL query that accesses my user identity.

In my master page I have a SqlDataSource that I want to compose a SQL query that returns an ID from a table

EDIT

OK, look at this example: (this is in Page_Load)

sqlDataSource1.SelectCommand = String.Format("select PageGroupID from users where Username = {0};", Page.User.Identity.Name);

But it doesn't work :(

Please help me!

Upvotes: 0

Views: 589

Answers (3)

Stack User
Stack User

Reputation: 1398

To understand difference try this;

string.Format("<br />Page.User.Identity: {0} " +  "<br />Request.LogonUserIdentity: {1}",
Page.User.Identity.Name, Request.LogonUserIdentity.Name);

Use Request.LogonUserIdentity.Name.

Upvotes: 0

podiluska
podiluska

Reputation: 51494

It won't work because you're constructing an invalid SQL string using string.format Try this, instead.

sqlDataSource1.SelectCommand =
     String.Format("select PageGroupID from users where Username = '{0}';", 
     Page.User.Identity.Name); 

Then when it works, go and read about SQL injection and why constructing SQL strings like this is A BAD IDEA.

Upvotes: 3

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Please reference this article - it should help you get the right value from Page.User.Identity.Name and then you can go from there by ensuring your database has matching values based on what it's returning.

Upvotes: 0

Related Questions