【问题标题】:How to get the username of windows authenticated user in asp.net?如何在asp.net中获取windows认证用户的用户名?
【发布时间】:2016-09-08 08:21:43
【问题描述】:

我有一个单页应用程序在我们的 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 中使用用户名,以便它可以作为其他调用服务器的参数发送。 我在网上搜索过,每个人都说获取登录用户的用户名是一个很大的安全漏洞。我确实看到了。但是当它以涉及服务器代码的方式完成时,我可能是一种解决方法?

有什么建议吗?

谢谢!

【问题讨论】:

  • 如果您在其他调用中将用户名作为参数发送,有什么办法可以阻止受过教育的用户用其他人的用户名替换该用户名?您的身份验证应在每次调用时进行。
  • 还有一点,您网站的身份验证设置是什么?您是否允许匿名身份验证?
  • 这是一个 Intranet 应用程序,用户具有良好的意图,因此安全性不是问题。我可以要求用户输入他的域用户名或创建登录表单或类似的东西,问题就会得到解决。我只是想让我的同事轻松一点,以便他们使用 Windows 域用户帐户登录一次。 Windows 身份验证在 IIS 中设置。目前不允许匿名访问。我会采取任何可行的黑客攻击,毕竟它都是内部的并且在防火墙后面。

标签: asp.net angularjs windows-authentication impersonation


【解决方案1】:

我在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)
        {

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2015-01-06
    • 2015-03-05
    相关资源
    最近更新 更多