自定义UserNamePasswordValidator.Validate从未在Visual Studio中命中 [英] Custom UserNamePasswordValidator.Validate never hit in Visual Studio

查看:81
本文介绍了自定义UserNamePasswordValidator.Validate从未在Visual Studio中命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试使用自定义UserNamePasswordValidator来验证凭据使用WCF服务时。但是,在Visual Studio 2017中永远不会调用Validate方法(.net framework 4.5.2)。

I'm trying to use a custom UserNamePasswordValidator to validate credentials when using a WCF service. However, the Validate method is never called in Visual Studio 2017 (.net framework 4.5.2).

这是我的自定义UserNamePasswordValidator类:

Here is my custom UserNamePasswordValidator class :

public class CustomUserNameValidator : UserNamePasswordValidator
    {
        public CustomUserNameValidator()
        {

        }

        public override void Validate(string userName, string password)
        {
            ILoginDomainProvider loginDomainProvider = NinjectWebCommon.Kernel.Get<ILoginDomainProvider>();
            if (loginDomainProvider.IsLoginValid(new Login(userName, password)))
                return;
            throw new SecurityTokenException("Account is invalid");
        } 
    }

LoginDomainProvider类只检查MySql数据库中是否存在用户名密码对。

The LoginDomainProvider class just checks the presence of the username password pair in a MySql database.

我的web.config:

My web.config :

<system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
            switchValue="Information, ActivityTracing"
            propagateActivity="true">
        <listeners>
          <add name="traceListener"
            type="System.Diagnostics.XmlWriterTraceListener"
            initializeData= "c:\temp\log\Traces.svclog" />
        </listeners>
      
      </source>
    </sources>
  </system.diagnostics>

< system.serviceModel>
< behavior>
< serviceBehaviors>

< behavior name =" ServiceBehaviorUsernameValidator">
< dataContractSerializer maxItemsInObjectGraph =" 6553500" />

< serviceMetadata httpGetEnabled =" true" httpsGetEnabled = QUOT;真" />
< serviceDebug includeExceptionDetailInFaults =" true" />
< serviceCredentials>
<! -
< clientCertificate>
< authentication certificateValidationMode =" None" />
< / clientCertificate>
- >
< userNameAuthentication
userNamePasswordValidationMode =" Custom"
customUserNamePasswordValidatorType =" Braille.WcfHost.CustomUserNameValidator,Braille.WcfHost" />
< serviceCertificate
findValue =" Braille.WcfHost"
x509FindType =" FindBySubjectName"
storeName =" My"
storeLocation =" CurrentUser" />
< / serviceCredentials>

<! -
< serviceAuthorization
principalPermissionMode =" UseAspNetRoles"
roleProviderName =" AspNetSqlRoleProvider" />
- >
< / behavior>

< behavior>
< serviceMetadata httpGetEnabled =" true" httpsGetEnabled = QUOT;真" />
< serviceDebug includeExceptionDetailInFaults =" true" />
< / behavior>
< / serviceBehaviors>
< / behavior>
< protocolMapping>
< add binding =" basicHttpBinding"方案= QUOT; HTTP" />
< add binding =" basicHttpsBinding"方案= QUOT; HTTPS" />
< / protocolMapping>
< bindings>

< wsHttpBinding>
< binding name =" WsHttpBindingConfig" maxBufferPoolSize = QUOT; 200000" maxReceivedMessageSize = QUOT; 200000"的SendTimeout = QUOT; 00:01:00">
< readerQuotas maxStringContentLength =" 200000" />
< security mode =" Message">
< message clientCredentialType =" UserName" />
< / security>
< / binding>
< / wsHttpBinding>
< / bindings>
< services>
< service name =" Braille.Services.ExerciseService" behaviorConfiguration = QUOT; ServiceBehaviorUsernameValidator">
< endpoint address =""结合= QUOT;&的wsHttpBinding QUOT; bindingName = QUOT; WsHttpBindingConfig"合同= QUOT; Braille.Contracts.IExerciseService" />
< / service>

< / services>
< serviceHostingEnvironment aspNetCompatibilityEnabled =" true" multipleSiteBindingsEnabled = QUOT;真" />
< /system.serviceModel>

<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="ServiceBehaviorUsernameValidator"> <dataContractSerializer maxItemsInObjectGraph="6553500"/> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceCredentials> <!-- <clientCertificate> <authentication certificateValidationMode="None"/> </clientCertificate> --> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Braille.WcfHost.CustomUserNameValidator,Braille.WcfHost"/> <serviceCertificate findValue="Braille.WcfHost" x509FindType="FindBySubjectName" storeName="My" storeLocation="CurrentUser" /> </serviceCredentials> <!-- <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="AspNetSqlRoleProvider" /> --> </behavior> <behavior> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpBinding" scheme="http" /> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <bindings> <wsHttpBinding> <binding name="WsHttpBindingConfig" maxBufferPoolSize="200000" maxReceivedMessageSize="200000" sendTimeout="00:01:00"> <readerQuotas maxStringContentLength="200000" /> <security mode="Message"> <message clientCredentialType="UserName"/> </security> </binding> </wsHttpBinding> </bindings> <services> <service name="Braille.Services.ExerciseService" behaviorConfiguration="ServiceBehaviorUsernameValidator"> <endpoint address="" binding="wsHttpBinding" bindingName="WsHttpBindingConfig" contract="Braille.Contracts.IExerciseService" /> </service> </services> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>

我的服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class ExerciseService : IExerciseService
    {
        private readonly IDomainProviderFactory _domainProviderFactory;

        public ExerciseService(IDomainProviderFactory domainProviderFactory)
        {
            Contract.Requires(domainProviderFactory != null);
            _domainProviderFactory = domainProviderFactory;
        }

        #region IExerciseService
        public List<LocalizedLabel> GetLabels()
        {
            var exerciseDomainProvider = _domainProviderFactory.CreateExerciseDomainProvider();
            var labels = exerciseDomainProvider.GetLabels();
            return labels;
        }

        public string Test()
        {
            return "test";
        }
        #endregion
    }




I已经看到多个与自定义UserNamePasswordValidator相关的帖子 被忽略,但他们涉及使用安全="运输"在IIS中,这与我的情况不同。

I've seen multiple posts related to custom UserNamePasswordValidator  being ignored, but they involved using security="Transport" in IIS, which is different from my case.

在CustomUserNameValidator的无参数构造函数上放置断点时,实际上在访问ExerciseService服务时会遇到它。如果我在< userNameAuthentication>中添加了无效信息(类名或汇编)部分,抛出异常
。所以,似乎我的课程得到了认可。但是,从我的服务调用方法时,从不会遇到Validate方法的断点,并且它们返回它们应该返回的任何结果,即使使用WcfTestClient.exe和不正确的
身份验证数据也是如此。

When putting a breakpoint on the parameterless constructor of CustomUserNameValidator, it is actually hit when accessing the ExerciseService service. If I put invalid information (class name or assembly) in the<userNameAuthentication> section, an exception is thrown. So, it seems my class is somehow recognized. However, the breakpoint on the Validate method is never hit when calling methods from my service, and they return whatever results they are supposed to return, even when using WcfTestClient.exe and incorrect authentication data.

我尝试在< security>中使用不同的模式section(我的安全模式的最终目标是TransportWithMessageCredential),将我的Validate方法的代码替换为抛出FaultException,或者注释< serviceCertificate>
部分尝试动摇,但没有任何变化。我的服务方法仍然使用无效的身份验证。我也不确定在这种情况下Traces.svclog如何(或者如果)可以帮助我。

I tried using different modes in the <security> section (my end goal for the security mode will be TransportWithMessageCredential) , replacing the code of my Validate method to just throwing a FaultException, or commenting the <serviceCertificate> section to try to shake things up, but nothing changes. My service methods still work with invalid authentication. I'm also unsure as to how (or if) the Traces.svclog can help me in this case.

我在这里不知所措。在WCF安全方面,我绝对不是专家,所以我可能会遗漏一些明显的东西,但如果有人能在这种情况下略微说清楚,我将不胜感激。

I'm at a loss here. I'm definitely not an expert when it comes to WCF security, so I'm probably missing something obvious, but I'd be grateful if someone could shed a bit of light in this case.

谢谢!

凯文










推荐答案

嗨Kevin,

Hi Kevin,

你的web.config中有问题。

There is something wrong in your web.config.

你应该对WsHttpBindingConfig使用"bindingConfiguration"而不是bindingName。

You should use "bindingConfiguration" for WsHttpBindingConfig instead of bindingName.

    <services>
      <service name="ExerciseService.Service1" behaviorConfiguration="ServiceBehaviorUsernameValidator">
        <endpoint address="" binding="wsHttpBinding"  bindingConfiguration="WsHttpBindingConfig" contract="ExerciseService.IService1" />
      </service>
    </services>

最好的问候,

Best Regards,

Edward


这篇关于自定义UserNamePasswordValidator.Validate从未在Visual Studio中命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆