[英]How to get the username of windows authenticated user in asp.net?
我的Intranet中運行着一個單頁應用程序。 用戶通過Windows身份驗證(其域用戶)進行身份驗證。 當單擊按鈕時,我要向具有以下代碼的aspx頁發送請求(使用$ http,Angular):
string result = "Unknown";
var loggedOnUser = System.Security.Principal.WindowsIdentity.GetCurrent();
if (loggedOnUser != null) {
int index = loggedOnUser.Name.LastIndexOf("\\", StringComparison.Ordinal) + 1;
result = loggedOnUser.Name.Substring(index);
}
var json = "{ \"User\" : \"" + result + "\"}";
Response.Clear();
Response.ContentType = "text/json";
Response.Write(json);
Response.End();
該代碼僅給我提供在應用程序池中注冊的用戶的名稱。 那的確不是很令人驚訝,所以我想我需要在這里做一些事嗎? 這樣做的原因是我希望在JavaScript中使用用戶名,以便在其他調用中將其作為參數發送到服務器。 我在網上搜索過,每個人都說獲取登錄用戶的用戶名是一個重大的安全漏洞。 我確實看到了。 但是當以涉及服務器代碼的方式完成時,我可能是一種解決方法?
有什么建議么?
謝謝!
我在Login
點擊時就這樣
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
string UserName = "";
string activeDomain = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string strName = HttpContext.Current.User.Identity.Name.ToString();
if (strName == "")
{
UserName = activeDomain;
}
else
{
UserName = strName;
}
if (UserName == "")
{
lblMsg.Text = "Invalid Credentials. Please contact administrator!";
}
else
{
LP.UserName = UserName;
DataSet dsUserName = LBLL.validate_user(LP);
if (dsUserName.Tables[0].Rows.Count > 0)
{
Session["UserName"] = dsUserName.Tables[0].Rows[0]["userName"].ToString();
Session["entityUID"] = dsUserName.Tables[0].Rows[0]["entityUID"].ToString();
Response.Redirect("~/index.aspx", false);
}
else
{
lblMsg.Text = "Invalid Credentials. Please contact administrator!";
}
}
}
catch (Exception ex)
{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.