gpt4 book ai didi

c# - 通过浏览器进行 WCF Rest 服务 Windows 身份验证

转载 作者:太空狗 更新时间:2023-10-29 23:30:58 26 4
gpt4 key购买 nike

Given 是一个 wcf rest 服务,它与 HttpClientCredentialType.Windows 一起运行,并强制用户通过 kerberos 进行身份验证。

        private static void Main(string[] args)
{
Type serviceType = typeof (AuthService);
ServiceHost serviceHost = new ServiceHost(serviceType);

WebHttpBinding binding = new WebHttpBinding();
binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

ServiceEndpoint basicServiceEndPoint = serviceHost.AddServiceEndpoint(typeof(IAuthService), binding, "http://notebook50:87");
basicServiceEndPoint.Behaviors.Add(new WebHttpBehavior());

Console.WriteLine("wcf service started");
serviceHost.Open();
Console.ReadLine();
}

public class AuthService : IAuthService
{
public List<string> GetUserInformation()
{
List<string> userInfo = new List<string>();
userInfo.Add("Environment.User = " + Environment.UserName);
userInfo.Add("Environment.UserDomain = " + Environment.UserDomainName);
if (OperationContext.Current != null && OperationContext.Current.ServiceSecurityContext != null)
{
userInfo.Add("WindowsIdentity = " + OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name);
userInfo.Add("Auth protocol = " + OperationContext.Current.ServiceSecurityContext.WindowsIdentity.AuthenticationType);
}
else
{
userInfo.Add("WindowsIdentity = empty");
}
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return userInfo;
}
}

[ServiceContract]
public interface IAuthService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "test/")]
List<string> GetUserInformation();
}

当我将其作为控制台应用程序运行,然后在另一台计算机的 Internet Explorer 中打开网站 http://notebook50:87/test/ 时,我收到“错误请求”响应。我确实启用了 kerberos 日志记录,它显示了 KDC_ERR_PREAUTH_REQUIRED

我可以通过创建一个 Windows 服务并在“本地系统帐户”下运行它来解决这个问题。在这种情况下,客户端能够进行身份验证。

问题:用户(运行此 wcf 服务的用户)需要什么权限/设置才能获得与应用程序在本地系统下作为 Windows 服务运行时相同的行为?这与服务主体名称有关吗?

最佳答案

它现在正在工作。确实是SPN的问题一开始,我将 SPN 设置为 setpn -A HTTP/notebook50.foo.com,这样,kerberos 身份验证就不起作用了。

现在,我将其设置为 setspn -A HTTP/notebook50.foo.com username,其中 username 是运行服务的用户。

根据我阅读的 SPN 文档,我不清楚我必须以这种方式设置用户帐户。

如果有人能解释这里发生的事情,并可能提供指向此场景的文档的链接,那就太好了。

关于c# - 通过浏览器进行 WCF Rest 服务 Windows 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com