好的!这是GET方法,尽管强烈建议不要在查询字符串中发送用户名和密码,但要为您提供启动器,您可以使用它。
定义你的合同:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebGet(UriTemplate = "/username={username}&password={password}",
ResponseFormat = WebMessageFormat.XML,
BodyStyle = WebMessageBodyStyle.Bare)]
string MyMethod(string username, string password);
}
而在你的实现方法,可以检索相同的用户名和密码。
public class MyServiceImpl:IMyService
{
public string MyMethod(string username, string password)
{
return "You entered "+username +" and "+password; // This will returned as an XML.
}
}
更改的Web.Config像:
<?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" maxRequestLength="2147483647" executionTimeout="300"/>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="" name="Namespace.MyService">
<endpoint address="" behaviorConfiguration="RESTBehavior" binding="webHttpBinding"
bindingConfiguration="webHttpBG" contract="Namespace.Name.IMyService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:51855/MyService.svc" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBG" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
maxBufferPoolSize="21474836470" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="RESTBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" bindingConfiguration="webHttpBG" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
并进行测试,您可以使用浏览器。在Web.Config文件中定义类型url。例如
http://localhost:51855/MyService.svc/username=myname&password=123
当你键入浏览器这个网址并回车就会打电话给你“的MyMethod”提供服务正在运行。
您也可以使用Fiddler来测试您的服务。
转到WCF REST。这很容易。 –
嗨Faizan谢谢!我正在使用WCF REST。你能解释一下如何传递用户名密码并检索服务中的相同内容。 – RamKr
感谢您的宝贵意见!我已经成功地在http请求中使用来自客户端html文件的Ajax/JQuery添加了Authorization标头,并且还检索了wcf端的用户凭证。现在还有一个问题想到,wcf方面有没有什么方法可以知道用户是否没有提供授权头并自动发送401,而不是进一步处理请求。 – RamKr