[英]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.