7

I have the following code to verify if the user has access to the application or not. Problem is System.Web.HttpContext.Current.User.Identity.Name returns empty. I checked. What could be the problem? My other application uses the same code snippet and it works there. Why is this happening?

string username = System.Web.HttpContext.Current.User.Identity.Name;
string str = "SELECT LASTNAME +', '+ FIRSTNAME AS NAME, USER_NAME, DEPARTMENT FROM DBNAME.DBO.TABLENAME WHERE USER_NAME = '" + username + "' ";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (rdr.HasRows == false)
{

        Server.Transfer("unauthorized.htm");
}
else
{
    while (rdr.Read())
    {
        name = rdr["NAME"].ToString();
        username = rdr["USER_NAME"].ToString();
        dept = rdr["DEPARTMENT"].ToString();
    }
}
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
Artemis
  • 403
  • 3
  • 10
  • 23

2 Answers2

5

It looks like you have an anonymous user. If you don't want to allow anonymous users, add the following to web.config:

<system.web>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>
Joe
  • 114,633
  • 27
  • 187
  • 321
0

If you are using Windows authetication with IIS Express, make sure you have in C:\Users\[username]\Documents\IISExpress\config\applicationhost.config .

Check related question: IIS Express gives Access Denied error when debugging ASP.NET MVC

Community
  • 1
  • 1
jumxozizi
  • 562
  • 8
  • 19