📄 header.ascx.cs
字号:
namespace RolesBasedAthentication.Web
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public abstract class Header : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.HyperLink Login;
protected System.Web.UI.WebControls.HyperLink Account;
protected System.Web.UI.WebControls.LinkButton SignOut;
protected System.Web.UI.WebControls.Label Roles;
protected System.Web.UI.WebControls.HyperLink RoleManager;
protected System.Web.UI.WebControls.Label Welcome;
private void Page_Load(object sender, System.EventArgs e)
{
if (Context.User.Identity.IsAuthenticated)
{
Login.Visible = false;
Welcome.Text = "Welcome " + ((SiteIdentity)Context.User.Identity).Name;
if (((SitePrincipal)Context.User).Roles.Count != 0)
{
Roles.Visible = true;
Roles.Text = "You are assigned to the following role(s):";
foreach (string role in ((SitePrincipal)Context.User).Roles)
Roles.Text += "<br>" + role;
if (((SitePrincipal)Context.User).IsInRole("Site Admin"))
RoleManager.Visible = true;
}
}
else
{
Account.Visible = false;
SignOut.Visible = false;
Welcome.Text = "Welcome, Guest";
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SignOut.Click += new System.EventHandler(this.SignOut_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SignOut_Click(object sender, System.EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut();
Response.Redirect("Default.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -