oAuth 2.0和C#WCF服务

时间:2014-01-09 19:34:36

标签: wcf rest oauth-2.0

我想在我的C#WCF Rest服务中加入一些安全性,我正在学习本教程: http://www.codeproject.com/Tips/372422/Secure-WCF-RESTful-service-using-OAUTH

如何升级这些信息以使用oAuth 2.0? 我下载了DotNetOpenAuth并将其安装到我的简单项目中。

这是我的 IRestService.cs

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WCFoAuth2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the      interface name "IRestService" in both code and config file together.
    [ServiceContract]
    public interface IRestService
    {
        [OperationContract]
         [WebInvoke(Method="GET",     ResponseFormat=WebMessageFormat.Xml,BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate="xml/    {id}")]
        string DoWork(string id);
    }
}

我的 RestService.svc

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFoAuth2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select RestService.svc or RestService.svc.cs at the Solution Explorer and start debugging.
    public class RestService : IRestService
    {
        public string DoWork(string id)
        {
            return id;
        }
    }
}

Web.config

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <authentication mode="Windows"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <system.webServer>
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WCFoAuth2.Service1Behavior" name="WCFoAuth2.Service1">
        <endpoint address="" binding="webHttpBinding" contract="WCFoAuth2.IService1" behaviorConfiguration="web">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="webHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service behaviorConfiguration="WCFoAuth2.RestServiceBehavior" name="WCFoAuth2.RestService">
        <endpoint address="" binding="webHttpBinding" contract="WCFoAuth2.IRestService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="webHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFoAuth2.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="WCFoAuth2.RestServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

韩国社交协会

0 个答案:

没有答案