Ruby: Get currently logged in user on windows

前端 未结 6 1437
不思量自难忘°
不思量自难忘° 2021-01-13 04:20

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?

FOR

6条回答
  •  迷失自我
    2021-01-13 04:39

    [RUBY ON RAILS ONLY]

    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:

    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.

提交回复
热议问题