Ruby: Get currently logged in user on windows

前端 未结 6 1438
不思量自难忘°
不思量自难忘° 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.

    0 讨论(0)
  • 2021-01-13 04:48

    To get the username of the current user on client machine you can use this

    ENV['USERNAME']

    0 讨论(0)
  • 2021-01-13 04:49

    If you're using Rails try: request.env['HTTP_REMOTE_USER']

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-13 04:55

    Well, to get the current username, there's this:

    puts ENV['USERNAME']
    

    Or go to the Win32API.

    require 'dl/win32'
    
    def get_user_name
      api = Win32API.new(
        'advapi32.dll',
        'GetUserName',
        'PP',
        'i'
      )
    
      buf = "\0" * 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

    Edit again: OK, it turns out I'm not an idiot after all. This is what 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.

    0 讨论(0)
  • 2021-01-13 04:55

    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:

    http://msdn.microsoft.com/en-us/magazine/cc163700.aspx (under Defining the sandbox).

    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.

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题