简体   繁体   English

使用表单身份验证获取Windows用户名的更好方法是什么?

[英]A better way to get a Windows Username using Forms Authentication?

I've inherited a project that uses forms authentication, but a recent feature request requires me to get the Windows username of some users. 我继承了一个使用表单身份验证的项目,但最近的功能请求要求我获取某些用户的Windows用户名。

It's a website that uses Forms authentication (and I do NOT want to change this, it would be too much work at this point) but I have had a feature request to allow our internal users to login more easily (usually without having to put in their password or username). 这是一个使用表单身份验证的网站(我不想改变它,此时会有太多工作)但我有一个功能请求,允许我们的内部用户更容易登录(通常无需放入他们的密码或用户名)。

My current method is HORRIBLE, it involves checking the ip address to see if they're connecting from one of our IPs, if they are, redirecting them to a separate project that uses Windows Authentication which then redirects them back to the original page with a query string containing their username (if I am forced to remain with this method I'll probably need to encrypt this although it's still not as secure as I would like). 我当前的方法是可怕的,它涉及检查IP地址以查看它们是否从我们的一个IP连接,如果是,将它们重定向到使用Windows身份验证的单独项目,然后将其重定向回原始页面包含其用户名的查询字符串(如果我被迫继续使用此方法,我可能需要加密它,尽管它仍然不如我想的那样安全)。

I can perform the windows authentication relatively easily inside my app, however the hard part is getting the windows username. 我可以在我的应用程序中相对容易地执行Windows身份验证,但是困难的部分是获取Windows用户名。 I could not figure out how to do it, hence my pretty ugly method. 我无法弄清楚如何做到这一点,因此我的丑陋方法。

Any help would be GREATLY appreciated. 任何帮助将不胜感激。

EDIT: My secondary app just does this: 编辑:我的辅助应用程序只是这样做:

string username = HttpContext.Current.User.Identity.Name;
string retpath = Request.QueryString["retpath"];
Response.Redirect("http://" + retpath + "?id=" + username);

This article contained the answer, (thanks vinodpthmn): http://msdn.microsoft.com/en-us/library/ms972958.aspx I'd found links to this article in my research, but none of them worked! 这篇文章包含了答案,(感谢vinodpthmn): http//msdn.microsoft.com/en-us/library/ms972958.aspx我在我的研究中找到了这篇文章的链接,但没有一个有效! Glad I was finally able to read it and solve my problem. 很高兴我终于能够阅读并解决我的问题了。

As I mentioned I only needed to get the windows username of clients, but that turned out to be quite troublesome, thankfully the actual solution was relatively simple. 正如我所提到的,我只需要获取客户端的Windows用户名,但结果证明这很麻烦,谢天谢地,实际的解决方案相对简单。

Firstly, you need to create an additional login page (I called mine winlogin.aspx) and set that as the default login page in your Web.Config file, like this: 首先,您需要创建一个额外的登录页面(我称之为winlogin.aspx)并将其设置为Web.Config文件中的默认登录页面,如下所示:

<authentication mode="Forms">
      <forms name="something" defaultUrl="default.aspx" loginUrl="winlogin.aspx" protection="All" slidingExpiration="true" timeout="90"/>
    </authentication>

Inside the winlogin.aspx.cs file is where you authenticate windows users and use 在winlogin.aspx.cs文件中,您可以对Windows用户进行身份验证并使用

FormsAuthentication.RedirectFromLoginPage(username, false/true);

To redirect them to wherever they were going. 将它们重定向到他们去的地方。 The actual winlogin.aspx page should be blank, users will never see it. 实际的winlogin.aspx页面应为空白,用户永远不会看到它。

You need to publish to your testing web server for this next part: You access the file properties of winlogin.aspx in IIS manager and set WindowsAuthentication to true and Anonymous Authentication to false. 您需要将以下部分发布到测试Web服务器:在IIS管理器中访问winlogin.aspx的文件属性,并将WindowsAuthentication设置为true,将匿名身份验证设置为false。 You also need to create a custom 401 error page pointer (mine points to my old Login.aspx page). 您还需要创建一个自定义401错误页面指针(我的指向旧的Login.aspx页面)。

Users that can login with windows authentication will do so, everybody else will be redirected to the old login page. 可以使用Windows身份验证登录的用户将这样做,其他人将被重定向到旧的登录页面。

Voila! 瞧!

There are a couple of issues however, you lose your return URL (you can't use "=" in the link to a custom 401 error page, so nothing can be parsed) so you'll probably want to set a cookie whenever people are redirected to the winlogin page and access that cookie in your forms login page to redirect the user appropriately. 但是有一些问题,你丢失了你的返回URL(你不能在自定义401错误页面的链接中使用“=”,所以没有任何东西可以被解析)所以你可能想要在人们设置cookie时被重定向到winlogin页面并访问表单登录页面中的cookie以适当地重定向用户。

If anybody has any questions, I suggest reading the article linked above, or feel free to ask questions here. 如果有人有任何疑问,我建议您阅读上面链接的文章,或者随时在这里提问。

Thanks everybody for your help! 谢谢大家的帮助!

You could use directory services 您可以使用目录服务

PrincipalContext pc = null;
UserPrincipal principal = null;
var username = User.Identity.Name;
pc = new PrincipalContext(ContextType.Domain, "give your domain name here");
principal = UserPrincipal.FindByIdentity(pc, userName);
var firstName = principal.GivenName ?? string.Empty;
var lastName = principal.Surname ?? string.Empty;

please add reference for System.DirectoryServices.AccountManagement 请添加System.DirectoryServices.AccountManagement的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM