views:

168

answers:

5

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 THOSE OF YOU SAYING IT IS IMPOSSIBLE, HERES PROOF:

http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx

A: 

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

YouMustLeaveNow
A: 

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.

Kenny Peng
+1  A: 

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.

AboutRuby
Just what I was looking for, I don't know why it didn't work the first time I tried it.
WedTM
A: 

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.

lusis
Nope, there is no client side code. I am sure of that.Here's a codeproject sample of what's going on. http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx
WedTM
A: 

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

ENV['USERNAME']

Rohit