is there a way to read a clients windows login name using ruby on rails

半世苍凉 提交于 2019-12-01 08:39:58

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.

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...

Flavio Wuensche

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:

If you don't care about these issues, go ahead:

  1. In your rails application, add Rekado's gem to your Gemfile: gem 'ntlm-sso', '=0.0.1'

  2. 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
    
  3. On your application.rb file, add the line: config.middleware.use "NTLMAuthentication"

  4. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!