Ruby:在 Windows 上获取当前登录的用户 [英] Ruby: Get currently logged in user on windows

查看:35
本文介绍了Ruby:在 Windows 上获取当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,我可以使用 HttpContext 获取 Web 应用程序的当前用户,但是,我不知道如何在 Ruby 中执行此操作.有没有办法做到这一点?

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?

对于那些认为不可能的人,这里有证据:

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

推荐答案

好吧,要获取当前用户名,有这个:

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

puts ENV['USERNAME']

或者转到 Win32API.

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

我是个白痴.这根本不是你要求的.哦,好吧,我花了一些时间从我的代码中挖掘出这一点,所以不妨留在这里让其他想知道的人:P

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

再次好吧,事实证明我毕竟不是白痴.这就是你想要的.当我回去重新阅读你的问题时,HttpContext 让我失望了,我认为这是来自 HTTP 身份验证或其他东西的当前用户名.

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.

这篇关于Ruby:在 Windows 上获取当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆