`IsAuthenticated`属性默认如何获得真值?

时间:2012-08-24 19:26:01

标签: c# asp.net .net

在Page_Load事件处理函数中,我尝试了以下内容:

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

它有效!浏览器显示Hello World!但默认情况下IsAuthenticated属性如何获得真值?

我的web.config文件如下:

<?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>

2 个答案:

答案 0 :(得分:3)

这取决于<authentication />元素的模式属性;如果是Windows,它将被设置为true,因为它使用当前的Windows用户;如果是表格,那么你必须登录。

设置表单身份验证的指南在此处:http://msdn.microsoft.com/en-us/library/ff647070.aspx

答案 1 :(得分:1)

添加以下内容:

<authentication mode="Windows"/>

<system.web>部分内的某个地方,如下所示:

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

更新您的示例:

<?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>