[英]Self-hosted WCF Server with SSO
我们有一个自托管的WCF服务,该服务通过HTTP托管内容,并希望能够支持Windows / AD的单点登录。 理想情况下,它将支持IE,Firefox和Chrome。
我构建了以下示例服务,该服务通过HTTP返回一些纯文本。 注意:在此版本的生产版本中,我们使用的是SSL,但我在下面将其关闭,以减少示例的运行难度。
我们将HttpSecurityMode设置为TransportCredentialOnly,然后将ClientCredentialType设置为HttpClientCredentialType.Windows,我相信它将使用Kerberos。
如果我使用“ NTLM”而不是“ Windows”,它似乎可以工作,但是我认为不建议使用NTLM,并且如果在我们的服务之前有一个反向代理,它就会中断。
当我们运行以下代码并连接到IE 10时,系统会提示您输入Windows凭据,但输入Windows凭据后,我们只会得到一个HTTP 400,并且在“ Get”方法中没有遇到任何断点。 理想情况下,我们应该看到的响应是:“您好,[域\\用户]!” 但我们没有做到那么远。
我们的测试机(客户端和服务器)都属于同一Windows域。 我以本地管理员而不是域管理员的身份运行服务(如果那很重要)。
我们将不胜感激!
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
namespace WindowsAuthService
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet(UriTemplate = "{*path}")]
Stream Get(string path);
}
public class TestService : ITestService
{
public Stream Get(string path)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.ContentType, "text/plain");
if (OperationContext.Current.ServiceSecurityContext == null)
return new MemoryStream(Encoding.ASCII.GetBytes(String.Format("Hello, {0}!", "Anonymous Stranger")));
else
return new MemoryStream(Encoding.ASCII.GetBytes(String.Format("Hello, {0}!", OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name)));
}
}
class Program
{
private const string URL = "http://mymachine.mydomain:7777";
static void Main(string[] args)
{
WebServiceHost serviceHost = new WebServiceHost(new TestService());
foreach (IServiceBehavior attr in serviceHost.Description.Behaviors)
{
if (attr is ServiceBehaviorAttribute)
{
ServiceBehaviorAttribute serviceAttr = (ServiceBehaviorAttribute)attr;
serviceAttr.InstanceContextMode = InstanceContextMode.Single;
serviceAttr.ConcurrencyMode = ConcurrencyMode.Multiple;
}
}
WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
ServiceEndpoint serviceEndpoint = serviceHost.AddServiceEndpoint(typeof (ITestService), binding, URL);
serviceEndpoint.Behaviors.Add(new WebHttpBehavior());
Console.WriteLine("Service Listening @ " + URL);
serviceHost.Open();
Console.WriteLine("[ Press Enter to Quit ]");
Console.ReadLine();
}
}
}
您已经有了工作代码。 我想你有代理人。 如果是,请在IE 10中转到菜单>工具> Internet选项>连接(选项卡)>局域网设置。 在对话框中,在“绕过代理服务器”上检查本地地址。
另外,您可以转到“安全性”选项卡>“本地Intranet”>“单击站点”按钮,然后选择“自动检测本地Intranet”-不会提示您输入凭据-IE 10将发送已登录用户的凭据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.