我只知道如何做到这一点与匿名用户,但也许我的设置将帮助你弄清楚。我知道下面的代码可以与匿名用户一起工作,也许你可以摆弄它以使其与认证一起工作。另外,保护Web端点的唯一方法是使用传输安全性通过HTTPS公开它(来源:http://msdn.microsoft.com/en-us/library/aa702697.aspx)。
的web.config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
Inferface
IExample.cs:
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WebPages.Interfaces
{
[ServiceContract]
public interface IExample
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json)]
string GetSomething(string id);
}
}
ExampleService.svc.cs标记
<%@ ServiceHost Language="C#" Debug="true" Service="WebPages.Interfaces.ExampleService" CodeBehind="ExampleService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
ExampleService.svc.cs代码隐藏
namespace WebPages.Interfaces
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ExampleService : IExample
{
string JsonSerializeSomething(Something something)
{
var serializer = new DataContractJsonSerializer(something.GetType());
var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, something);
return Encoding.Default.GetString(memoryStream.ToArray());
}
public string GetSomething(string id)
{
var something = DoSomeBusinessLogic(id);
return JsonSerializeSomething(something);
}
}
}
从客户jQuery的通话
function _callServiceInterface(id, delegate) {
var restApiCall = "Interfaces/ExampleService.svc/GetSomething?id="
+ escape(id);
$.getJSON(restApiCall, delegate);
}
function _getSomethingFromService() {
_callServiceInterface('123',
function (result) {
var parsedResult = $.parseJSON(result);
$('#info').html(result.SomethingReturnedFromServiceCall);
}
);
}
如果您可以添加有关您遇到的特定问题的详细信息,那么任何人都会回答您的问题。现在,这本身并不是一个问题。 – 2009-11-01 22:12:44
对不起,缺乏细节!基本上,我想要一个WinGUI应用程序可以调用的安全服务来将交易和其他相关的“任务”发布到Azure队列中。所有的任务都是无国籍和幂等的。几十个用户都需要将凭据传递给服务才能进行身份验证和授权。至于安全模式,我更喜欢消息安全性,但也可以使用TranportWithMessageCredential安全性。除了那些我愿意使用.NET服务总线的人,还没有得到一个适合我的例子。 – 2009-11-02 13:39:37
您是否正在寻找仅使用用户名和密码进行WCF呼叫的身份验证? – BozoJoe 2012-01-25 00:32:38