问题
I've built an intranet application on Ruby on Rails running on Windows. Everyone who will be accessing this app is running on Windows and is already logged into the network with Windows.
All I need to do is to grab the login name of the logged in user and save that login name to a session variable. I tried ENV['USERNAME'] but that only shows the person who is logged into the server box, which is me.
Is there any way to automatically grab and store that variable from the users who are access the site? I know that I could ask them for it but I assume that there must be some way to grab it.
Thanks...Chris
回答1:
No, you will need to setup an authentication system within your rails application. Accessing the clients environment would be a huge security breach in a number of systems such as the browser. Depending on your requirements there are a number of nice plugins for rails to handle all of this authentication for you. I personally like devise. There are a few plugins for it and one that would authenticate with an LDAP server if you did not wish to store username/passwords and make users maintain a separate set of login credentials for your intranet application. This, however, is very common.
回答2:
This site has a purported answer, but it's IE-only. And since browser's really aren't supposed to make that sort of information available, this may have been removed from newer versions of IE. The post's from late 2008, so...
As much as I hate IE and platform-dependentness, if you're stuck on a Windows-only, IE-only corporate intranet anyway, this might be the way to go.
Hope this helps!
PS: If people will just be accessing your app through their work machines, what about using MAC / local (desktop) IP addresses? MAC would be more reliable, of course, but it's also IE only...
回答3:
Same question here: Rails get username of currently logged windows user
Anyways, just copying my own answer below...
This is what worked for me but there are some limitations:
- won't work in Chrome: undefined method 'encode' for nil:NilClass
- won't validate user credentials
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.rbwith: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 endOn your
application.rbfile, add the line:config.middleware.use "NTLMAuthentication"Call
request.env["REMOTE_USER"]on your view or controller to get current username.
PS: Let me know if you find anyway to make it work on Chrome or to validate user credentials.
来源:https://stackoverflow.com/questions/5506932/is-there-a-way-to-read-a-clients-windows-login-name-using-ruby-on-rails