2012-08-24 61 views
0

在Page_Load事件處理函數,我試過如下:默認情況下`IsAuthenticated`屬性如何獲得真實值?

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> 
+1

你使用Windows身份驗證? –

+0

恐怕我不知道。我只是選擇創建一個新的ASP.NET空網站。我不知道它是Windows還是指定表單身份驗證。 –

+0

檢查你的web.config文件。 –

回答

3

這取決於<authentication />元素的模式屬性;如果是Windows,它將被設置爲true,因爲它使用當前的Windows用戶;如果表單,那麼你必須先登錄

的指南來設置窗體身份驗證是在這裏:http://msdn.microsoft.com/en-us/library/ff647070.aspx

+0

看我編輯了我的問題並附加了我的web.config文件,這裏沒有規範。 –

+0

你需要指定它。 http://support.microsoft.com/kb/301240 – BNL

+0

@BNL我的問題是爲什麼它不需要我的web.config中的規範? –

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> 
相關問題