0

在 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>
4

2 回答 2

3

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

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

于 2012-08-24T19:28:33.337 回答
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>
于 2012-08-24T20:28:53.133 回答