c# - 使用 wshttpbinding 的 WCF 用户名和密码验证不起作用
问题描述
我是 WCF 服务身份验证的新手,我试图使用 wshttpbinding 实现 wcfauthentication。但我得到了例外。
找不到与具有绑定 WSHttpBinding 的端点的方案 https 匹配的基地址。注册的基地址方案是 [http]。
网络配置:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttp">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WCFAuth.Service1" behaviorConfiguration="wsHttpBehavior">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp" contract="WCFAuth.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:64765/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="wsHttpBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFAuth.ServiceAuthanticator, WCFAuth"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
服务认证类:
using System;
using System.Collections.Generic;
using System.IdentityModel.Selectors;
using System.Linq;
using System.ServiceModel;
using System.Web;
namespace WCFAuth
{
public class ServiceAuthanticator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
string AppUserName = "ABC";
string AppPwd = "abc";
try
{
if (userName.ToLower() != AppUserName.ToLower() && password != AppPwd)
{
throw new FaultException("Unknown Username or Incorrect Password");
}
}
catch (Exception ex)
{
throw new FaultException("Unknown Username or Incorrect Password");
}
}
}
}
客户端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<!--<binding name="base" />-->
<binding name="base">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:64765/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="base" contract="WCFAuth.IService1" name="base" />
</client>
</system.serviceModel>
</configuration>
消费者:
class Program
{
static void Main(string[] args)
{
try
{
WCFAuth.Service1Client client = new WCFAuth.Service1Client();
client.ClientCredentials.UserName.UserName = "test";
client.ClientCredentials.UserName.Password = "test";
var temp = client.GetData(1);
Console.WriteLine(temp);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
当我尝试浏览 svc 文件时出现附加异常。
有人可以纠正我,我在哪里犯错,在此先感谢。
解决方案
这里的问题是您使用的是具有传输安全性的WSHttpBinding,但您设置的基地址是 http。此处无法使用 http,因为您通过网络发送凭据。
要么将其更改为 https,要么为开发目的创建第二个绑定配置。一个具有传输安全性 (https),另一个没有 (http)。
还要确保您的客户端绑定与您的服务器的绑定相匹配。
推荐阅读
- php - 无法从 php 容器连接到 mysql 容器
- qt - 为什么 QFontMetrics 提供的宽度与 Qml Rectangle/Text 显示的宽度不匹配
- django - 从 django 1..x 和 cms-plugins 迁移到 djangocms 插件和 django 2 后出错
- python - 有没有办法使用余弦相似度找到 BallTree 或 KDTree 的最近邻居?
- javascript - 在 JavaScript 数组中将连续数字分组为“from~to”字符串
- php - 在函数内逐行读取 .txt 文件 - 优化 PHP 脚本以获得更好的性能
- javascript - 从一组按钮转移到另一组按钮
- angular - Angular 服务调用测试
- swiftui - 您可以将 UIViewRepresentable (UISearchBar) 添加为 navigationBarItem 吗?- 斯威夫特用户界面
- amazon-web-services - 如何通过powershell创建AWS New-Scprovisionedproduct