Ruby:在 Windows 上获取当前登录的用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3251757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby: Get currently logged in user on windows
提问by WedTM
In C# I can get the current user of a web app using the HttpContext, however, I can't figure out how to do this in Ruby. Is there any way of doing this?
在 C# 中,我可以使用 HttpContext 获取 Web 应用程序的当前用户,但是,我不知道如何在 Ruby 中执行此操作。有没有办法做到这一点?
FOR THOSE OF YOU SAYING IT IS IMPOSSIBLE, HERES PROOF:
对于那些说这是不可能的人,这里有证据:
http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx
http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx
回答by AboutRuby
Well, to get the current username, there's this:
好吧,要获取当前用户名,请执行以下操作:
puts ENV['USERNAME']
Or go to the Win32API.
或者去Win32API。
require 'dl/win32'
def get_user_name
api = Win32API.new(
'advapi32.dll',
'GetUserName',
'PP',
'i'
)
buf = "require 'rack'
require 'rack/auth/ntlm-sso'
class NTLMAuthentication
def initialize(app)
@app = app
end
def call(env)
auth = Rack::Auth::NTLMSSO.new(@app)
return auth.call(env)
end
end
" * 512
len = [512].pack('L')
api.call(buf,len)
buf[0..(len.unpack('L')[0])]
end
puts get_user_name
Edit: And I'm an idiot. This isn't what you asked for at all. Oh well, it took me time to dig this out of my code, so it might as well stay here for anyone else wondering :P
编辑:我是个白痴。这根本不是你要的。哦,好吧,我花了一些时间从我的代码中挖掘出来,所以它可能会留在这里让其他想知道的人:P
Edit again: OK, it turns out I'm not an idiot after all. This iswhat you want. When I went back and re-read your question, the HttpContext threw me off, and I thought it was the current username from HTTP auth or something.
再次编辑:好吧,事实证明我毕竟不是白痴。这就是你想要的。当我回去重新阅读你的问题时, HttpContext 把我吓跑了,我认为这是来自 HTTP auth 或其他东西的当前用户名。
回答by Rohit
To get the username of the current user on client machine you can use this
要获取客户端计算机上当前用户的用户名,您可以使用它
ENV['USERNAME']
ENV['用户名']
回答by Flavio Wuensche
[RUBY ON RAILS ONLY]
[仅铁路上的红宝石]
This is what worked for me but there are some limitations:
这对我有用,但有一些限制:
If you don't care about these issues, go ahead:
如果您不关心这些问题,请继续:
In your rails application, add Rekado's gem to your Gemfile:
gem 'ntlm-sso', '=0.0.1'
Create an initialiser
config/initializers/ntlm-sso.rb
with:require 'rack' require 'rack/auth/ntlm-sso' class NTLMAuthentication def initialize(app) @app = app end def call(env) auth = Rack::Auth::NTLMSSO.new(@app) return auth.call(env) end end
On your
application.rb
file, add the line:config.middleware.use "NTLMAuthentication"
Call
request.env["REMOTE_USER"]
on your view or controller to get current username.
在您的 rails 应用程序中,将 Rekado 的 gem 添加到您的 Gemfile 中:
gem 'ntlm-sso', '=0.0.1'
使用以下命令创建初始化程序
##代码##config/initializers/ntlm-sso.rb
:在您的
application.rb
文件中,添加以下行:config.middleware.use "NTLMAuthentication"
调用
request.env["REMOTE_USER"]
您的视图或控制器以获取当前用户名。
PS: Let me know if you find anyway to make it work on Chrome or to validate user credentials.
PS:如果您发现无论如何要使其在 Chrome 上运行或验证用户凭据,请告诉我。
回答by YouMustLeaveNow
If you're using Rails try: request.env['HTTP_REMOTE_USER']
如果您使用 Rails,请尝试: request.env['HTTP_REMOTE_USER']
回答by Kenny Peng
I think what you mean is how you can retrieve the username that the user used to login to the web application. That will differ depending on what authentication mechanism you're using. Some Apache authentication modules, for example, will pass REMOTE_USER (e.g. the Kerberos module), the CAS Single-Sign-On module passes CAS-USER, etc. Standard digest authentication and such uses the Authentication header. You should be able to access these using request.env[HEADER] as someone else pointed out above. Check out the documentation on how your authentication layer is passing on the user in the HTTP request.
我认为您的意思是如何检索用户用于登录 Web 应用程序的用户名。这将根据您使用的身份验证机制而有所不同。例如,某些 Apache 身份验证模块将通过 REMOTE_USER(例如 Kerberos 模块),CAS 单点登录模块通过 CAS-USER 等。标准摘要身份验证等使用 Authentication 标头。正如上面其他人指出的那样,您应该能够使用 request.env[HEADER] 访问这些。查看有关您的身份验证层如何在 HTTP 请求中传递给用户的文档。
回答by lusis
Is your c# code running as a .NET plugin/client-side code or is it ENTIRELY server side? Your ruby code would be entirely server side. According to the MS docs, only stuff running in the CLR sandbox can really get to that information:
您的 c# 代码是作为 .NET 插件/客户端代码运行还是完全是服务器端?您的 ruby 代码将完全是服务器端。根据 MS 文档,只有在 CLR 沙箱中运行的东西才能真正获得该信息:
http://msdn.microsoft.com/en-us/magazine/cc163700.aspx(under Defining the sandbox).
http://msdn.microsoft.com/en-us/magazine/cc163700.aspx(在定义沙箱下)。
One thing interesting to note is that sites registered under LocalIntranet have access to that information. I'm not sure off hand how this maps to security zones in IE though.
需要注意的一件有趣的事情是在 LocalIntranet 下注册的站点可以访问该信息。不过,我不确定这如何映射到 IE 中的安全区域。
The thing to understand is that LOGON_USER is NOT visible to the browser sandbox anymore than the browser can see the contents of a filesystem path on your system. The fact that your c# code sees it almost certainly indicitive of some clientside component passing it upstream.
需要理解的是 LOGON_USER 对浏览器沙箱不可见,就像浏览器可以看到系统上文件系统路径的内容一样。事实上,您的 c# 代码几乎可以肯定某些客户端组件将它传递给上游。
You have the option of implementing mod_ntlm under apache and pushing the headers downstream. I don't have the points to post a second link but google 'rails ntlm sso' and see the rayapps.com link.
您可以选择在 apache 下实现 mod_ntlm 并将标头推送到下游。我没有资格发布第二个链接,但谷歌“rails ntlm sso”并查看 rayapps.com 链接。
but if your app isn't Rails based, you'll have to port that to your server code. You can also checkout rack-ntlm if your app is rack compliant.
但如果您的应用程序不是基于 Rails 的,则必须将其移植到您的服务器代码中。如果您的应用程序兼容机架,您还可以检查 rack-ntlm。