WCF: Cannot find my custom validator specified in web.config - customUserNamePasswordValidatorType - - Could not load file or assembly … - help?

て烟熏妆下的殇ゞ 提交于 2019-12-05 12:33:24

I decided to give it another stab, and didn't like having my custom validator in another lib.

So I created a new class in App_Code, and went at it...

The following is what actually fixed it,

="CustomValidator.CustomUserNameValidator, App_Code"

When you refer to the custom validator with the values

="CustomValidator.CustomUserNameValidator, CustomValidator"

The first value is the type name and the second is the name of the assembly in which to find the type. So I would suggest that in your first instance your service is actually in some other assembly such as MyService In that case you really needed your config file to say

="CustomValidator.CustomUserNameValidator, MyService"

I suspect that when you have created your new class library for your validator, you have called your project CustomValidator (which will output an assembly called CustomValidator.dll), and hence now your config will work (i.e. it has nothing to do with being in a separate class library - it just happens that the naming of your assembly reference in the web.config is now valid)

ElHaix

Seems a bit strange, but the solution was to create a separate class library and make reference to its DLL in my WCF service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.ServiceModel;

/// <summary>
/// Summary description for CustomUsernamePasswordValidator
/// </summary>
namespace CustomValidator
{
    public class CustomUserNameValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }


            if (!AuthenticateUser(userName, password))
                throw new SecurityTokenValidationException("Invalid Credentials");
            else
            {
                // do nothing - they're good
            }
        }

        public bool AuthenticateUser(string userName, string password)
        {
            if (userName != "userbill" || password != "passwordbill")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

I then made added a reference to System.IdentityModel and System.ServiceModel.

The serviceCredentials section for the WCF service is now changed to this:

<serviceCredentials>
    <!-- Use our own custom validation -->
    <userNameAuthentication userNamePasswordValidationMode="Custom"
                            customUserNamePasswordValidatorType="CustomValidator.CustomUserNameValidator, CustomValidator"/>
</serviceCredentials>

Hope that helps someone.

I tried this with invalid credentials, and was expecting to see my "Invalid Credentials" message. Instead I'm getting "At least one security token in the message could not be validated."

Other than that this thing is finally up and running!

Just reading this as it was helpful for a POC I had to get going quickly. In response to ELHaix above...this should work to ensure your descriptive custom error is returned back to the client:

using System.ServiceModel
...    
throw new FaultException("Invalid Credentials - Custom Error");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!