問題是您將實際的User.Identity.Name
作爲字符串而不是其值。
SelectCommand = String.Format("SELECT * FROM member WHERE username = '{0}'", User.Identity.Name)
但更好(更安全)的做法是像
SelectCommand = "SELECT * FROM member WHERE username = @UserName"
SelectCommand.Parameters.AddWithValue("@UserName", User.Identity.Name)
這將防止SQL注入。
編輯: 既然你在你的頁面定義這一點,你可以用下面的模板:
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:Details %>" SelectCommand="SELECT * FROM members WHERE ([username] = @UserName)">
<SelectParameters>
<asp:Parameter Name="UserName" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
,則默認值設置爲User.Identity.Name
在服務器端:
SqlDataSource1.SelectParameters.Item(0).DefaultValue = User.Identity.Name
更簡單的方法是使用Configure Data Source
嚮導,該向導通過在設計視圖中單擊SqlDataSource對象旁邊的右箭頭可用。
它說的名字 「大衛」 當我跑在Visual Studio System.Web.Security.FormsIdentity調試器| {} System.Web.Security.FormsIdentity名 = 「大衛」 – user1625271
authenticationtype = 「形式」 isAuthenticated =「 true「 – user1625271