[英]How do you get a User Identity?
一旦對用戶進行身份驗證和授權,應用程序便可以使用User對象的Identity屬性獲取有關該用戶的信息。 Identity屬性返回一個包含用戶名和角色信息的對象。
以下是我用來理解該概念的代碼段:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = User.Identity.IsAuthenticated.ToString();
Label2.Text = User.Identity.Name;
Label3.Text = User.Identity.AuthenticationType;
}
還有其他方法可以獲取用戶身份嗎?
請在下面說明是否需要以下內容。
如果只希望在調用Page_Load時從ASP.NET WebPage中獲得用戶Identity ..,請創建一個string []並執行以下操作
string strRawUser = Page.User.Identity.Name;
然后從那里strRawUser將有類似“ DomainName \\ UserName”的內容,因此您需要將字符串拆分為stringArray並獲取string [1]值,如下所示
string[] strRawUserSplitter = Page.User.Identity.Name.Split("\\");
Label2.Text = strRawUserSplitter[1]
您還可以使用Principal Object獲取當前用戶身份。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.