简体   繁体   English

获取登录用户

[英]Get Logged In User

My company has a large application we are finishing up. 我公司的大型应用程序正在整理中。 It utilizes WCF as the back-bone with Active Directory for authentication. 它利用WCF作为Active Directory的骨干进行身份验证。 This works perfectly for one of the two sites as the authentication model is Windows and you have to be part of the domain to sign into the site. 这对于两个站点之一非常适用,因为身份验证模型是Windows,并且您必须是域的一部分才能登录该站点。 The question I have relates to the other site which is externally accessible. 我所提出的问题与可以从外部访问的其他站点有关。 It sets ClientCredentials.Windows.ClientCredential for the proxy call with a specific user/pass for impersonation of an AD-user so the full security model works. 它将为代理调用设置ClientCredentials.Windows.ClientCredential,并使用特定用户/通行证来模拟AD用户,以便使用完整的安全模型。 This all works exactly as expected too. 这一切也完全符合预期。

The question I have is, on the web I can use HttpContext.Current.User.Identity.Name to get the currently logged in user from the Forms authentication piece, but to do this I have to make sure a System.Web reference exists against the DLL I'm currently working in. Our base objects come from a simplistic class that doesn't know about System.Web. 我的问题是,在网络上我可以使用HttpContext.Current.User.Identity.Name从Forms身份验证部分获取当前登录的用户,但是要执行此操作,我必须确保针对System.Web引用存在我当前正在使用的DLL。我们的基础对象来自一个不了解System.Web的简单类。 Is there a way to find out the Forms user who is logged in inside that base object project? 有没有办法找出在该基础对象项目中登录的Forms用户? I tried System.Security.Principal but that only gives me access to the Windows accounts from what I could tell and won't do me any good. 我尝试了System.Security.Principal,但是这只能使我从所能知道的范围访问Windows帐户,对我没有任何帮助。

I know an option is to just reference System.Web and be done with it but that sounds really klugy to me and not the best option so I'm hoping for some tips here. 我知道一个选择是仅引用System.Web并完成它,但这对我来说确实是个难题,而不是最好的选择,所以我希望在此提供一些提示。

Thread.CurrentPrincipal.Identity will do the exact same thing as HttpContext.CurrentContext... Thread.CurrentPrincipal.Identity将执行与HttpContext.CurrentContext...完全相同的操作HttpContext.CurrentContext...

It will return the identity associated with the current executing thread, which in most cases, is the logged on user*. 它将返回与当前执行线程关联的身份,在大多数情况下,该身份是登录的用户*。

Note: *If you are using delegation/impersonation or running as a service account, it will return the account of which ever identity the thread is under, but in your case, it doesn't sound like you are doing any context identity switching. 注意:*如果您使用委派/模拟或作为服务帐户运行,它将返回该线程所在身份的帐户,但是在您的情况下,这听起来并不像您在进行任何上下文身份切换。

If user name as a string is enough for you, 如果用户名作为字符串就足够了,

Environment.UserName
// Environment.UserDomainName and MachineName can also be useful

From MSDN : MSDN

Gets the user name of the person who started the current thread. 获取启动当前线程的人员的用户名。

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

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