首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF .net 4.0HTTP基本验证

WCF .net 4.0HTTP基本验证
EN

Stack Overflow用户
提问于 2011-05-04 00:31:26
回答 3查看 9.7K关注 0票数 5

我已经找了好几天了。

我只是尝试使用基本的HTTP身份验证将用户名/密码传递到我的RESTful服务中。其他的都运行得很棒!

下面是我的web.config的样子:

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>

    <bindings>
      <wsHttpBinding>
        <binding name="">
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ParkingPandaREST.CustomUserNameValidator, ParkingPandaREST" />
          </serviceCredentials>      
        </behavior>
      </serviceBehaviors>
    </behaviors>


  </system.serviceModel>

</configuration>

然后,我创建了一个包含以下内容的CustomUserNameValidator类:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ParkingPandaREST
{
    public class CustomUserNameValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
    {
        // This method validates users. It allows in two users, test1 and test2 
        // with passwords 1tset and 2tset respectively.
        // This code is for illustration purposes only and 
        // must not be used in a production environment because it is not secure.    
        public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }

            if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset"))
            {
                // This throws an informative fault to the client.
                throw new System.ServiceModel.FaultException("Unknown Username or Incorrect Password");
                // When you do not want to throw an infomative fault to the client,
                // throw the following exception.
                // throw new SecurityTokenException("Unknown Username or Incorrect Password");
            }
        }
    }
}

我只是在我的本地机器上运行这个服务,并试图在CustomUserNameValidator类中找到一个中断点,但它从未到达那里。因此,我现在没有使用SSL,只是简单地尝试获取传入的用户名/密码。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-05 11:03:05

这是我想出来的答案。它可能需要进行一些调整,但它是您完成工作所需的核心。

注意:关闭IIS中的基本身份验证

代码语言:javascript
复制
           // Get the Header Authorization Value
            string headerValue = operationContext.IncomingRequest.Headers[HttpRequestHeader.Authorization];
            headerValue = headerValue.Replace("Basic ", "");
            headerValue = DecodeBase64String(headerValue);

            // Get Username and Password
            string userName = headerValue.Substring(0, headerValue.IndexOf(":"));
            string password = headerValue.Substring(headerValue.IndexOf(":") + 1, headerValue.Length - userName.Length - 1);

            // Do Custom Authorization here with the userName and password

.

这里还有DecodeBase64String函数

代码语言:javascript
复制
    public static string DecodeBase64String(string encodedData)
    {
        byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
        string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
        return returnValue;
    }

希望这能帮助其他有同样问题的人--干杯!

票数 4
EN

Stack Overflow用户

发布于 2011-05-04 01:21:41

WCF4中HTTP传输的默认绑定是basicHttpBinding。如果您将wsHttpBinding元素转换为basicHttpBinding元素并将其配置为自定义验证,那么您的代码应该会执行。

票数 1
EN

Stack Overflow用户

发布于 2011-05-04 03:03:29

基本身份验证是HTTP协议。因此,它是传输安全,而不是消息安全。您的配置应包含以下代码片段:

代码语言:javascript
复制
<security mode="Transport">
    <transport clientCredentialType="Basic" />
</security>

但是即使你做对了,你的自定义代码可能也不会被调用。您将在此blog中找到有关此问题的更多详细信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5872679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档