Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Get currently logged in user on windows

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

like image 340
WedTM Avatar asked Jul 15 '10 01:07

WedTM


2 Answers

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.

like image 120
AboutRuby Avatar answered Nov 11 '22 10:11

AboutRuby


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

ENV['USERNAME']

like image 38
Rohit Avatar answered Nov 11 '22 08:11

Rohit