I need to get the username of the currently logged windows user. Could it be done easily?
-
possible duplicate of [is there a way to read a clients windows login name using ruby on rails](http://stackoverflow.com/questions/5506932/is-there-a-way-to-read-a-clients-windows-login-name-using-ruby-on-rails) – bummi Jun 15 '15 at 23:08
4 Answers
The username of the account running the script can be accessed via something like:
puts ENV['USERNAME']
Beware that if you're running it as a system service the username will probably come back as "SYSTEM"
If that isn't enough to suite your needs there is an alternative method outlined here: https://stackoverflow.com/a/3544741/648695
- 1
- 1
- 8,315
- 7
- 52
- 71
You can use the Ruby etc module
require 'etc'
Etc.getlogin
The doc is avaiable here: http://ruby-doc.org/stdlib-2.0.0/libdoc/etc/rdoc/Etc.html#method-c-getlogin
Returns the short user name of the currently logged in user.
- 6,051
- 4
- 48
- 67
As far as I know, unless you're using active directory that would require a microsoft framework website, I don't think you'll find a way in rails.
There are a few discussion points here that may help: Can you get a Windows (AD) username in PHP?
Same question here: is there a way to read a clients windows login name using ruby on rails
Anyways, just copying my own answer below...
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.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.
- 1
- 1
- 9,460
- 1
- 57
- 54