简体   繁体   English

如何在Windows上运行的自托管ServiceStack中获取用户名

[英]How do I get the username in a self-hosted ServiceStack running on Windows

I'm building a Windows service and instead of exposing a WCF or .Net remoting interface, I'm giving ServiceStack a shot. 我正在构建一个Windows service ,而不是公开WCF或.Net远程处理界面,我给了ServiceStack一个机会。 (So far, I'm digging it!) (到目前为止,我正在挖掘它!)

I need to get the Username of the user calling the service. 我需要获取调用该服务的Username的用户名。 The user will have already been authenticated against Active Directory so I don't want them to see any additional screens. 用户已经针对Active Directory进行了身份验证,因此我不希望他们看到任何其他屏幕。

The service will be called by a Winforms application so I COULD pass in the username, but that could be spoofed . 该服务将通过一个WinForms应用程序调用,所以我COULD在用户名通过,但可能是spoofed

any ideas? 有任何想法吗?

Okay, so basically this ain't supported by ServiceStack. 好的,所以ServiceStack基本上不支持这个。 But you can get the username by doing like this. 但你可以通过这样做获得用户名。

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>
   {

   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户名/密码凭据从php客户端传递到自托管的wcf服务? - How do I pass username/password credentials from php client to self-hosted wcf service? 如何远程访问自托管的Nancy服务? - How do I remotely access self-hosted Nancy service? 自托管服务器中的ServiceStack会话为null - ServiceStack Session is null in self-hosted server 如何在自托管的.net核心mvc应用程序中运行长时间运行或重复运行的任务? - How should I run long-running or recurring taks in a self-hosted .net core mvc application? 如何在自托管 (AppSelfHostBase) 服务堆栈服务 (RequestStream) 上设置文件大小限制? - How to set a file size limit on a self-hosted (AppSelfHostBase) Servicestack service (RequestStream)? 如何安全退出自托管应用程序(退出前先做点事情) - How can I safe to quit self-hosted application (do something before quit) 如何在自托管 CoreWCF 服务器中获取调用者身份? - How to get caller identity in a self-hosted CoreWCF server? 具有每个请求生存期范围的ServiceStack自托管应用程序 - ServiceStack self-hosted application with per-request lifetime scope 具有嵌入式images / css的ServiceStack Razor(自托管) - ServiceStack Razor (self-hosted) with embedded images/css 在自托管控制台应用程序中使用ServiceStack Mini Profiler - Using ServiceStack Mini Profiler in self-hosted console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM