我只知道如何做到這一點與匿名用戶,但也許我的設置將幫助你弄清楚。我知道下面的代碼可以與匿名用戶一起工作,也許你可以擺弄它以使其與認證一起工作。另外,保護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