显示/隐藏站点地图,如果用户登录
本文关键字:如果 用户 登录 地图 隐藏 站点 显示 | 更新日期: 2023-09-27 18:17:48
我有一个登录页面,后面的代码如下:
protected void btnLog_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection("Data Source=GATE-PC''SQLEXPRESS;Initial Catalog=dbUsers;Integrated Security=True");
conn1.Open();
SqlCommand cmdd = new SqlCommand("select * from Users where UserName = @user AND Password = @pass", conn1);
SqlParameter param = new SqlParameter();
SqlParameter param1 = new SqlParameter();
param.ParameterName = "@user";
param1.ParameterName = "@pass";
param.Value = txtuser.Text;
param1.Value = txtpass.Text;
cmdd.Parameters.Add(param);
cmdd.Parameters.Add(param1);
SqlDataReader reader = cmdd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
MessageBox("Login Successful");
clear();
}
else
{
MessageBox("Invalid Username/Password");
}
}
我有两个站点地图:
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="False" IncludeStyleBlock="False" Orientation="Horizontal"
DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2"
onmenuitemdatabound="NavigationMenu_MenuItemDataBound">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<div class="clear hideSkiplink">
<asp:Menu ID="Menu1" runat="server" CssClass="menu"
EnableViewState="False" IncludeStyleBlock="False" Orientation="Horizontal"
DataSourceID="SiteMapDataSource2" StaticDisplayLevels="2"
onmenuitemdatabound="NavigationMenu_MenuItemDataBound">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" SiteMapProvider="AdminSiteMapProvider"/>
我想要实现的是,当用户不登录在Menu1将显示和NavigationMenu将隐藏,但如果用户登录在Menu1将隐藏然后NavigationMenu将显示。
我正在使用asp.net和c#。
if (reader.HasRows)
{
reader.Read();
// set as user authenticated
FormsAuthentication.SetAuthCookie(txtuser.Text, true);
MessageBox("Login Successful");
clear();
}
在你的PageLoad
protected void Page_Load(object sender, EventArgs e)
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Menu1.Visible = false;
NavigationMenu.Visible = true;
}
else
{
Menu1.Visible = true;
NavigationMenu.Visible = false;
}
}
或
protected void Page_Load(object sender, EventArgs e)
{
bool isAuthenticated = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
Menu1.Visible = !isAuthenticated;
NavigationMenu.Visible =isAuthenticated;
}
首先,您最好使用ASP。NET成员资格。从您的示例中,看起来您将密码存储在纯文本中,这很糟糕。关于如何设置asp.net成员,请阅读下面的内容。
然后,为了实现基于用户是否经过身份验证的不同视图,您可以使用asp.net LoginView控件,如下所示:
<asp:LoginView ID="LoginView0" runat="server">
<AnonymousTemplate>
<asp:Menu ID="Menu1" runat="server" CssClass="menu"
EnableViewState="False" IncludeStyleBlock="False" Orientation="Horizontal"
DataSourceID="SiteMapDataSource2" StaticDisplayLevels="2"
onmenuitemdatabound="NavigationMenu_MenuItemDataBound">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" SiteMapProvider="AdminSiteMapProvider"/>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="False" IncludeStyleBlock="False" Orientation="Horizontal"
DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2"
onmenuitemdatabound="NavigationMenu_MenuItemDataBound">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
</LoggedInTemplate>
</asp:LoginView>