WCF .net 4.5 – HTTPS Restful with Windows Authentication
web.conf |
<?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> <services> <service name="WcfServiceWinAuth.Service" behaviorConfiguration="webHttpBehaviorConfig"> <endpoint address="" binding="webHttpBinding" contract="WcfServiceWinAuth.IService" behaviorConfiguration="web" bindingConfiguration="webHttpBindingConfig"> </endpoint> </service> </services> <bindings> <webHttpBinding> <binding name="webHttpBindingConfig" crossDomainScriptAccessEnabled="false"> <security mode="Transport"> <transport clientCredentialType="Windows" proxyCredentialType="Windows" /> </security> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="webHttpBehaviorConfig"> <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <serviceCredentials> <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true" /> </serviceCredentials> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="web"> <webHttp /> </behavior> </endpointBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <directoryBrowse enabled="true"/> </system.webServer> </configuration> |
IService.cs |
using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; namespace WcfServiceWinAuth { [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedResponse, UriTemplate = "Item/{id}")] ItemDataResponse GetItem(string id); } [DataContract] public class ItemDataResponse { … … } } |
Service.svc.cs |
using System; using System.ServiceModel; using System.ServiceModel.Web; namespace WcfServiceWinAuth { public class Service : IService { [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedResponse, UriTemplate = "Item/{id}")] public ItemDataResponse GetItem(string id) { return new ItemDataResponse(id, System.Web.HttpContext.Current.User.Identity.Name); } } } |