HTTP错误401.2 - 未经授权由于身份验证标头无效,您无权查看此页面。(对于基本身份验证)

时间:2015-10-06 06:29:36

标签: c# asp.net

我想为我的服务启用“基本”身份验证。禁用匿名和Windows身份验证。并在applicationhost.config中启用“basicAuthentication”,但我无法访问该服务。低于错误。

HTTP错误401.2 - 未经授权由于身份验证标头无效,您无权查看此页面。

我正在使用IIS Express 10.这是我的applicationhost.config

<authentication>

                <anonymousAuthentication enabled="false" userName="" />

                <basicAuthentication enabled="true" />

                <clientCertificateMappingAuthentication enabled="false" />

                <digestAuthentication enabled="false" />

                <iisClientCertificateMappingAuthentication enabled="false">
                </iisClientCertificateMappingAuthentication>

                <windowsAuthentication enabled="false">
                    <providers>
                        <add value="Negotiate" />
                        <add value="NTLM" />
                    </providers>
                </windowsAuthentication>

            </authentication>

这是我的Web.config

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>

  <!--Enable directory browsing in the Server-->
  <system.webServer>
    <directoryBrowse enabled="true"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>



  <system.serviceModel>

    <bindings>
      <webHttpBinding>
        <binding name="webHttpTransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="Basic"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>



    <services>
      <service name="MyWCFServices.DemoREST" behaviorConfiguration="DemoREST">

        <!--Define base https base address-->
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:44300/HelloWorldService.svc/"/>
          </baseAddresses>
        </host>

        <!--webHttpBinding allows exposing service methods in a RESTful manner-->
        <endpoint name="rest" address="" binding="webHttpBinding" 
             contract="MyWCFServices.IDemoREST" bindingConfiguration="webHttpTransportSecurity" 
                  behaviorConfiguration="DemoREST"/> 
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>      
        <behavior name="DemoREST">
          <!-- To avoid disclosing metadata information, set the value below to
         false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true. 
         Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>

          <!--Using custom username and Password-->
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyWCFServices.CustomUserNameValidator, HelloWorldService" />
          </serviceCredentials>
          <serviceAuthenticationManager authenticationSchemes="Basic"/>
        </behavior>

      </serviceBehaviors>

      <!--Required default endpoint behavior when using webHttpBinding-->
      <endpointBehaviors>
        <behavior name="DemoREST">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

    </behaviors>

  </system.serviceModel>
</configuration>

自定义验证器类:

namespace MyWCFServices
{
    class CustomUserNameValidator : UserNamePasswordValidator
    {
        // This method validates users. It allows in two users, user1 and user2
        // 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("You must provide both the username and password to access this service");
            }

            if (!(userName == "user1" && password == "test") && !(userName == "user2" && password == "test"))
            {
                // This throws an informative fault to the client.
                throw new FaultException("Unknown Username or Incorrect Password");
                // When you do not want to throw an informative fault to the client,
                // throw the following exception.
                // throw new SecurityTokenException("Unknown Username or Incorrect Password");
            }
        }

    }

}

如果我在任何地方都错了,请指导我。

0 个答案:

没有答案