如果您对当多个用户在Windows中login时如何获取活动用户?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于当多个用户在Windows中login时如何获取活动用户?的
如果您对当多个用户在Windows中login时如何获取活动用户?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于当多个用户在Windows中login时如何获取活动用户?的详细内容,我们还将为您解答usergroupinformation 多用户的相关问题,并且为您提供关于.net – 使用Windows身份验证时如何模拟其他Windows用户?、Java 如何获取活动用户的UserDetails、Ruby:在Windows上获取当前login的用户、winapi – 获取Windows中登录用户的用户名/密码的有价值信息。
本文目录一览:- 当多个用户在Windows中login时如何获取活动用户?(usergroupinformation 多用户)
- .net – 使用Windows身份验证时如何模拟其他Windows用户?
- Java 如何获取活动用户的UserDetails
- Ruby:在Windows上获取当前login的用户
- winapi – 获取Windows中登录用户的用户名/密码
当多个用户在Windows中login时如何获取活动用户?(usergroupinformation 多用户)
假设在Windows上有多个用户login。 说, user1login,然后switch user和switch userlogin,(不使user1注销)。 假设有一个应用程序在用户login时运行。 有两个用户user1和user2login, user2作为活动用户,并且有两个应用程序。
我的问题是:该应用程序如何知道其相应的用户是否活跃? 也就是说, user2域中的应用程序确定其用户处于活动状态,而user1域中的应用程序确定其用户当前处于非活动状态。 谢谢!
sql错误不传播批次
转义序列 a不工作
我应该如何与Win32 C ++应用程序集成并打包第三方库?
Windows Phone SDK – XAML – 上边距的自动保证金
如何将string发送到包括Microsoft Word在内的其他应用程序
您可以调用WTSGetActiveConsoleSessionId以获取物理控制台上当前处于活动状态的终端服务(又名“快速用户切换”(即“远程桌面”)会话标识。
您可以使用WTS_CURRENT_SESSION为会话标识调用WTSQuerySessioninformation ,为WTSSessionId使用WTSInfoClass来获取当前进程的终端服务会话标识。
如果活动会话标识和当前进程会话标识相同,则当前进程对应的用户在物理控制台上具有活动会话。
如果您想知道当前进程正在运行的会话是否处于活动状态(但不一定在物理控制台上),则可以使用WTSConnectState选项来WTSQuerySessioninformation 。
从Windows服务运行时,WTSGetActiveConsoleSessionId()实际上可能会返回会话0。 如果您需要从Windows服务中执行此操作,则需要枚举所有会话并查找连接的会话,然后从中获取用户。
下面的代码做的比这更多,包括模拟这个用户,并以windows用户的身份运行一个进程,但是如果你只是对用户名感兴趣,请查找第二个调用WTSQuerySessioninformation()函数的实例。
//Function to run a process as active user from windows service void ImpersonateActiveUserAndRun(WCHAR* path,WCHAR* args) { DWORD session_id = -1; DWORD session_count = 0; WTS_SESSION_INFOA *pSession = NULL; if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,1,&pSession,&session_count)) { //log success } else { //log error return; } for (int i = 0; i < session_count; i++) { session_id = pSession[i].SessionId; WTS_CONNECTSTATE_CLASS wts_connect_state = WTSdisconnected; WTS_CONNECTSTATE_CLASS* ptr_wts_connect_state = NULL; DWORD bytes_returned = 0; if (::WTSQuerySessioninformation( WTS_CURRENT_SERVER_HANDLE,session_id,WTSConnectState,reinterpret_cast<LPTSTR*>(&ptr_wts_connect_state),&bytes_returned)) { wts_connect_state = *ptr_wts_connect_state; ::WTSFreeMemory(ptr_wts_connect_state); if (wts_connect_state != WTSActive) continue; } else { //log error continue; } HANDLE hImpersonationToken; if (!WTSQueryUserToken(session_id,&hImpersonationToken)) { //log error continue; } //Get real token from impersonation token DWORD neededSize1 = 0; HANDLE *realToken = new HANDLE; if (GetTokeninformation(hImpersonationToken,(::TOKEN_informatION_CLASS) TokenLinkedToken,realToken,sizeof(HANDLE),&neededSize1)) { CloseHandle(hImpersonationToken); hImpersonationToken = *realToken; } else { //log error continue; } HANDLE hUserToken; if (!DuplicatetokenEx(hImpersonationToken,//0,//MAXIMUM_ALLOWED,TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS | MAXIMUM_ALLOWED,NULL,SecurityImpersonation,TokenPrimary,&hUserToken)) { //log error continue; } // Get user name of this process //LPTSTR pUserName = NULL; WCHAR* pUserName; DWORD user_name_len = 0; if (WTSQuerySessioninformationW(WTS_CURRENT_SERVER_HANDLE,WTSUserName,&pUserName,&user_name_len)) { //log username contained in pUserName WCHAR string } //Free memory if (pUserName) WTSFreeMemory(pUserName); ImpersonateLoggedOnUser(hUserToken); STARTUPINFOW StartupInfo; GetStartupInfoW(&StartupInfo); StartupInfo.cb = sizeof(STARTUPINFOW); //StartupInfo.lpDesktop = "winsta0\default"; PROCESS_informatION processInfo; Security_ATTRIBUTES Security1; Security1.nLength = sizeof Security_ATTRIBUTES; Security_ATTRIBUTES Security2; Security2.nLength = sizeof Security_ATTRIBUTES; void* lpEnvironment = NULL; // Get all necessary environment variables of logged in user // to pass them to the new process BOOL resultEnv = CreateEnvironmentBlock(&lpEnvironment,hUserToken,FALSE); if (!resultEnv) { //log error continue; } WCHAR PP[1024]; //path and parameters ZeroMemory(PP,1024 * sizeof WCHAR); wcscpy(PP,path); wcscat(PP,L" "); wcscat(PP,args); // Start the process on behalf of the current user BOOL result = CreateProcessAsUserW(hUserToken,PP,//&Security1,//&Security2,FALSE,norMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,//lpEnvironment,//"C:\ProgramData\some_dir",&StartupInfo,&processInfo); if (!result) { //log error } else { //log success } DestroyEnvironmentBlock(lpEnvironment); CloseHandle(hImpersonationToken); CloseHandle(hUserToken); CloseHandle(realToken); RevertToSelf(); } WTSFreeMemory(pSession); }
.net – 使用Windows身份验证时如何模拟其他Windows用户?
认证是实现这一目标的方式,我该如何解决?
解决方法
如果您还需要模拟,可以将web.config中identity元素中的impersonate属性设置为true,但是从您提到的内容来看,听起来根本不需要模拟.
Java 如何获取活动用户的UserDetails
在我的控制器中,当我需要活动(登录)用户时,我正在执行以下操作以获取UserDetails实现:
User activeUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();log.debug(activeUser.getSomeCustomField());
它工作正常,但我认为Spring在这种情况下可以使生活更轻松。有没有办法将UserDetails自动接线连接到控制器或方法中?
例如,类似:
public ModelAndView someRequestHandler(Principal principal) { ... }
但是UsernamePasswordAuthenticationToken
我得到了,UserDetails
而不是得到了?
我正在寻找一种优雅的解决方案。有任何想法吗?
答案1
小编典典序言:从Spring-Security 3.2开始@AuthenticationPrincipal,此答案的末尾描述了一个不错的注释。当你使用Spring-Security> = 3.2时,这是最好的方法。
当你:
使用较旧版本的Spring-Security,
需要通过存储在主体中的某些信息(例如登录名或ID)从数据库加载你的自定义用户对象,
想要了解a HandlerMethodArgumentResolver
或如何WebArgumentResolver
以一种优雅的方式解决此问题,或者只是想要了解@AuthenticationPrincipaland
的背景AuthenticationPrincipalArgumentResolver
(因为它基于HandlerMethodArgumentResolver
)
然后继续阅读-否则,请使用@AuthenticationPrincipal
并感谢Rob Winch
(的作者@AuthenticationPrincipal
)和Lukas Schmelzeisen
(的回答)。
(顺便说一句:我的回答有点老(2012年1月),因此是Lukas Schmelzeisen作为第一个使用@AuthenticationPrincipal基于Spring Security 3.2 的注释解决方案的人。)
然后你可以在控制器中使用
public ModelAndView someRequestHandler(Principal principal) { User activeUser = (User) ((Authentication) principal).getPrincipal(); ...}
如果你需要一次,那没关系。但是,如果你因其丑陋的原因而需要它的几倍,因为它会污染你的控制器的基础结构详细信息,通常框架应该将其隐藏。
因此,你可能真正想要的是拥有这样的控制器:
public ModelAndView someRequestHandler(@ActiveUser User activeUser) { ...}
因此,你只需要实现即可WebArgumentResolver。它有一种方法
Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception
这将获取Web请求(第二个参数),并且User如果感觉对方法参数(第一个参数)负责,则必须返回。
从Spring 3.1开始,有一个新概念称为HandlerMethodArgumentResolver。如果使用Spring 3.1+,则应该使用它。(此答案的下一部分对此进行了说明)
public class CurrentUserWebArgumentResolver implements WebArgumentResolver{ Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { if(methodParameter is for type User && methodParameter is annotated with @ActiveUser) { Principal principal = webRequest.getUserPrincipal(); return (User) ((Authentication) principal).getPrincipal(); } else { return WebArgumentResolver.UNRESOLVED; } }}
你需要定义自定义注释-如果应始终从安全上下文中获取User的每个实例,但绝不要将其作为命令对象,则可以跳过它。
@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ActiveUser {}
在配置中,你只需要添加以下内容:
<beanid="applicationConversionService"> <property name="customArgumentResolver"> <bean/> </property></bean>
@See:学习自定义Spring MVC @Controller方法参数
应该注意的是,如果你使用的是Spring 3.1,他们建议使用HandlerMethodArgumentResolver而不是WebArgumentResolver。-见周杰伦的评论
与HandlerMethodArgumentResolverSpring 3.1+
相同
public class CurrentUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter methodParameter) { return methodParameter.getParameterAnnotation(ActiveUser.class) != null && methodParameter.getParameterType().equals(User.class); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (this.supportsParameter(methodParameter)) { Principal principal = webRequest.getUserPrincipal(); return (User) ((Authentication) principal).getPrincipal(); } else { return WebArgumentResolver.UNRESOLVED; } }}
在配置中,你需要添加此
<mvc:annotation-driven> <mvc:argument-resolvers> <bean/> </mvc:argument-resolvers> </mvc:annotation-driven>
@See 利用Spring MVC 3.1 HandlerMethodArgumentResolver接口
Spring-Security 3.2解决方案
spring安全3.2(不使用Spring 3.2混淆)在溶液中有自己的编译:@AuthenticationPrincipal(org.springframework.security.web.bind.annotation.AuthenticationPrincipal)。Lukas Schmelzeisen的回答对此进行了很好的描述。
只是在写
ModelAndView someRequestHandler(@AuthenticationPrincipal User activeUser) { ... }
要使此工作正常进行,你需要注册AuthenticationPrincipalArgumentResolver(org.springframework.security.web.bind.support.AuthenticationPrincipalArgumentResolver)
:通过“激活” @EnableWebMvcSecurity
或在其中注册此Bean,mvc:argument-resolvers
这与我在上面的Spring 3.1解决方案中描述的方式相同。
@请参阅《Spring Security 3.2参考》的第11.2章。@AuthenticationPrincipal
Spring-Security 4.0解决方案
它的工作方式类似于Spring 3.2解决方案,但在Spring 4.0中,@AuthenticationPrincipal和AuthenticationPrincipalArgumentResolver
被“移动”到另一个软件包中:
org.springframework.security.core.annotation.AuthenticationPrincipal
org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver
(但是旧包中的旧类仍然存在,因此请不要混合使用!)
只是在写
import org.springframework.security.core.annotation.AuthenticationPrincipal;ModelAndView someRequestHandler(@AuthenticationPrincipal User activeUser) { ...}
要使此工作正常进行,你需要注册(org.springframework.security.web.method.annotation.)AuthenticationPrincipalArgumentResolver
:通过“激活” @EnableWebMvcSecurity
或在其中注册此Bean,mvc:argument-resolvers
这与我在上面的Spring 3.1解决方案中描述的方式相同。
<mvc:annotation-driven> <mvc:argument-resolvers> <bean/> </mvc:argument-resolvers></mvc:annotation-driven>
Ruby:在Windows上获取当前login的用户
在C#中,我可以使用HttpContext获取Web应用程序的当前用户,但是,我无法弄清楚如何在Ruby中执行此操作。 有没有办法做到这一点?
对于你们说这是不可能的,这是certificate:
http://www.codeproject.com/KB/aspnet/How_to_NT_User_Name.aspx
在Apache上使用mod_auth_sspi和mod_wsgi运行两个Django应用程序
我可以grep telnet命令输出?
PHP单点login使用NTLMv2
在C#中获取本地Windows用户login会话时间戳
检查用户是否是组的成员
sql Server 2005login错误
找出用户属于哪个组
任何Windows API让我重新login一个“locking”的Windows 7?
通过VB.NET在Windows上login
为没有login的用户设置环境variables
那么,要获得当前的用户名,就是这样的:
puts ENV['USERNAME']
或者去Win32API。
require 'dl/win32' def get_user_name api = Win32API.new( 'advapi32.dll','GetUserName','PP','i' ) buf = "" * 512 len = [512].pack('L') api.call(buf,len) buf[0..(len.unpack('L')[0])] end puts get_user_name
编辑:而我是一个白痴。 这不是你所要求的。 哦,我花了我的时间来从代码中挖掘出来,所以不妨留在这里,让其他人想知道:P
再次编辑:好的,事实证明,我并不是一个白痴。 这是你想要的。 当我回去重读你的问题,HttpContext把我扔了,我认为这是从HTTP身份验证或什么的当前用户名。
要获取当前用户在客户机上的用户名,你可以使用这个
ENV [ 'USERNAME']
如果您使用Rails,请尝试: request.env['HTTP_REMOTE_USER']
我想你的意思是如何检索用户用来登录到Web应用程序的用户名。 这将根据您使用的认证机制而有所不同。 例如,某些Apache身份验证模块将传递REMOTE_USER(例如Kerberos模块),CAS单一登录模块传递CAS-USER等。标准摘要身份验证等使用Authentication标头。 你应该可以像上面指出的那样使用request.env [HEADER]来访问它们。 查看有关您的身份验证层在HTTP请求中如何传递给用户的文档。
你的C#代码是作为一个.NET插件/客户端代码运行吗?还是全部是服务器端? 你的ruby代码将完全是服务器端。 根据MS文档,只有在CLR沙盒中运行的东西才能真正得到这些信息:
http://msdn.microsoft.com/en-us/magazine/cc163700.aspx (在定义沙箱下)。
有一点需要注意的是,在Localintranet下注册的站点可以访问这些信息。 我不知道这是如何映射到IE中的安全区域。
要理解的是,浏览器沙箱不再可见logoN_USER,而浏览器不能看到系统上文件系统路径的内容。 事实上,你的C#代码几乎可以肯定某些客户端组件将其传递到上游。
你可以选择在apache下执行mod_ntlm,然后向下游推送标题。 我没有要点发表第二个链接,但谷歌的铁轨ntlm Sso',看到rayapps.com链接。
但是如果你的应用程序不是基于Rails的,你必须把它移植到你的服务器代码中。 如果您的应用符合机架标准,您也可以结帐rack-ntlm。
[仅在铁轨上运行]
这是对我有用,但有一些限制:
将无法在Chrome中工作: undefined method 'encode' for nil:NilClass
将不验证用户凭证
如果您不关心这些问题,请继续:
在您的Rails应用程序中,将Rekado的gem添加到您的Gemfile:gem'ntlm gem 'ntlm-sso','=0.0.1'
创建一个初始化config/initializers/ntlm-sso.rb与:
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
在您的application.rb文件中,添加以下行: config.middleware.use "NTLMAuthentication"
在视图或控制器上调用request.env["REMOTE_USER"]以获得当前用户名。
PS:让我知道如果你发现无论如何,使其在Chrome上工作或验证用户凭据。
winapi – 获取Windows中登录用户的用户名/密码
先谢谢你.
或者,如果你想像Novell那样实现某种“单点登录”系统,你应该通过GINA(Vista之前)或Credential Provider(Vista)来实现,这将导致您的代码被赋予用户名和登录时的密码,密码可用的唯一时间.
对于用户名,获取当前用户名(运行代码的用户名)很简单:AdvApi32.dll中的GetUserName函数可以为您完成此操作.
如果您作为服务运行,则需要记住没有人“登录用户”:除了任何实际人员之外,还有几个在任何时候,例如LocalSystem,NetworkService,SYstem和其他帐户. This article provides some sample code and documentation这样做.
关于当多个用户在Windows中login时如何获取活动用户?和usergroupinformation 多用户的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.net – 使用Windows身份验证时如何模拟其他Windows用户?、Java 如何获取活动用户的UserDetails、Ruby:在Windows上获取当前login的用户、winapi – 获取Windows中登录用户的用户名/密码的相关信息,请在本站寻找。
本文标签: