Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

How the `IsAuthenticated` property gets true value by default?

In the Page_Load event handler function, I tried the following:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Context.User.Identity.IsAuthenticated)
        {
            Response.Write("Hello World");
        }
    }

And It works ! the browser shows Hello World ! but how the IsAuthenticated property gets true value by default?

My web.config file is as follows:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <connectionStrings>
        <add name="arsenicDesktopConnectionString" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=arsenicDesktop;Persist Security Info=True;User ID=sa;Password=1234"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <compilation debug="false" targetFramework="4.0" />
    </system.web>
    <!--LOCAL APPLICATION SETTINGS-->
  <appSettings>
    <add key="Accounts_SettingsFile" value="C:\Users\user\Documents\Visual Studio 2010\WebSites\UserAuthenticationSystem\Config\Accounts.Config"/>
  </appSettings>
</configuration>

Upvotes: 0

Views: 363

Answers (2)

Brian Mains
Brian Mains

Reputation: 50728

It depends on the <authentication /> element's mode property; if Windows, it will be set to true as it uses the current windows user; if forms, then you have to be logged in.

A guide to setting up forms authentication is here: http://msdn.microsoft.com/en-us/library/ff647070.aspx

Upvotes: 3

Mark
Mark

Reputation: 1537

Add the following:

<authentication mode="Windows"/>

somewhere within the <system.web> section, like this:

<configuration>
    <system.web>
        <authentication mode="Windows"/>
    <system.web>
</configuration>

Updating your example:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <connectionStrings>
        <add name="arsenicDesktopConnectionString" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=arsenicDesktop;Persist Security Info=True;User ID=sa;Password=1234"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <compilation debug="false" targetFramework="4.0" />
        <authentication mode="Windows"/>
    </system.web>
    <!--LOCAL APPLICATION SETTINGS-->
  <appSettings>
    <add key="Accounts_SettingsFile" value="C:\Users\user\Documents\Visual Studio 2010\WebSites\UserAuthenticationSystem\Config\Accounts.Config"/>
  </appSettings>
</configuration>

Upvotes: 1

Related Questions