servicestack,C#,Winforms,Security,servicestack" /> servicestack,C#,Winforms,Security,servicestack" />

C# 如何在Windows上运行的自托管服务堆栈中获取用户名

C# 如何在Windows上运行的自托管服务堆栈中获取用户名,c#,winforms,security,servicestack,C#,Winforms,Security,servicestack,我正在构建一个Windows服务,而不是公开WCF或.netremoting接口,我正在尝试ServiceStack。(到目前为止,我正在挖掘!) 我需要获取调用服务的用户的用户名。用户已经通过Active Directory的身份验证,因此我不希望他们看到任何其他屏幕 Winforms应用程序将调用该服务,因此我可以传入用户名,但这可能是欺骗的 有什么想法吗?好的,所以ServiceStack基本上不支持这一点。 但是你可以这样做来获得用户名 namespace ServiceStackNTM

我正在构建一个
Windows服务
,而不是公开WCF或.netremoting接口,我正在尝试
ServiceStack
。(到目前为止,我正在挖掘!)

我需要获取调用服务的用户的
用户名。用户已经通过Active Directory的身份验证,因此我不希望他们看到任何其他屏幕

Winforms应用程序将调用该服务,因此我
可以
传入用户名,但这可能是
欺骗的


有什么想法吗?

好的,所以ServiceStack基本上不支持这一点。 但是你可以这样做来获得用户名

namespace ServiceStackNTML
{
   using ServiceStack.Common;
   using ServiceStack.ServiceHost;
   using ServiceStack.ServiceInterface;
   using ServiceStack.WebHost.Endpoints;
   using System;
   using System.Net;

   class Program
   {
       static void Main(string[] args)
       {
           var host = new NTMLAppHost();
           host.Init();
           host.Start("http://localhost:8080/");

           Console.ReadLine();
       }
   }

   class NTMLAppHost : AppHostHttpListenerBase
   {
       public NTMLAppHost() : base("Test", typeof(NTMLAppHost).Assembly) { }

       public override void Configure(Funq.Container container)
       {

       }

       public override void Start(string urlBase)
       {
           base.Start(urlBase);
           this.Listener.AuthenticationSchemes = System.Net.AuthenticationSchemes.Ntlm;
       }

       protected override void ProcessRequest(HttpListenerContext context)
       {
           HostContext.Instance.Items["User"] = context.User;
           base.ProcessRequest(context);
       }
   }

   class TestService : Service
   {
       public string Any(UsernameRequest request)
       {
           return ((System.Security.Principal.IPrincipal)HostContext.Instance.Items["User"]).Identity.Name;
       }
   }

   [Route("/Username")]
   class UsernameRequest : IReturn<string>
   {

   }
}
namespace-ServiceStackNTML
{
使用ServiceStack.Common;
使用ServiceStack.ServiceHost;
使用ServiceStack.ServiceInterface;
使用ServiceStack.WebHost.Endpoints;
使用制度;
Net系统;
班级计划
{
静态void Main(字符串[]参数)
{
var host=new NTMLAppHost();
host.Init();
host.Start(“http://localhost:8080/");
Console.ReadLine();
}
}
类NTMLAppHost:AppHostHttpListenerBase
{
public NTMLAppHost():base(“Test”,typeof(NTMLAppHost).Assembly{}
公共覆盖无效配置(Funq.Container)
{
}
公共覆盖无效开始(字符串urlBase)
{
base.Start(urlBase);
this.Listener.AuthenticationSchemes=System.Net.AuthenticationSchemes.Ntlm;
}
受保护的覆盖无效ProcessRequest(HttpListenerContext上下文)
{
HostContext.Instance.Items[“User”]=context.User;
ProcessRequest(上下文);
}
}
类TestService:Service
{
公共字符串Any(UsernameRequest请求)
{
return((System.Security.Principal.IPrincipal)HostContext.Instance.Items[“User”]).Identity.Name;
}
}
[路线(“/Username”)]
类UsernameRequest:IReturn
{
}
}

这是不可能的。用户不会“呼叫”服务。服务使用您自己的用户帐户(通常为“系统”)在其自己的独立会话中运行。如果这一点很重要,那么您不应该使用服务,而应该使用一个常规程序,该程序在用户的启动文件夹中以快捷方式启动。用户将使用一个调用我的服务公开的方法的程序,类似于RPC调用。使用.net远程处理,我可以从线程中获取凭据。不确定您是否可以使用ServiceStack即时获取凭据。看看这条线: