Use Custom Authorization Provider causes Exception

Topics: Security Application Block
Feb 14, 2011 at 11:37 AM
Edited Feb 14, 2011 at 11:38 AM

Hi,

I created a custom Authorization Provider to declare authorization via database.

Everytime I try to get the provider via ProviderFactory the following ArgumentException is thrown.

 

The type 'SqlServerAuthorizationProviding.SqlServerAuthorizationProvider, SqlServerAuthorizationProviding, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.

 

Here the applicationcode :

 

var principal = new WindowsPrincipal(new WindowsIdentity(userName));

try
{
  var authorizations = AuthorizationFactory.GetAuthorizationProvider(); // <-- Exception
  return authorizations.Authorize(principal, action);
}
catch (Exception ex)
{
}

 

Here the code of the provider:

 

    [ConfigurationElementType(typeof(SqlServerAuthorizationProviderData))]
    public class SqlServerAuthorizationProvider : IAuthorizationProvider
    {
        private UserAccessData userAccessData = new UserAccessData();

        public SqlServerAuthorizationProvider(string connectionStringName)
        {
            GenericDataAccess dbAccess = null;

            if (String.IsNullOrEmpty(connectionStringName))
                dbAccess = new GenericDataAccess(true);
            else
                dbAccess = new GenericDataAccess(connectionStringName);

            var userData = dbAccess.ExecuteTableForCommand("Select * From TestUser");

            foreach (DataRow user in userData.Rows)
                this.userAccessData.User.AddUserRow((int)user[0], (string)user[1], (string)user[2], (string)user[3]);

            var moduleData = dbAccess.ExecuteTableForCommand("Select * From TestModule");

            foreach (DataRow module in moduleData.Rows)
                this.userAccessData.Module.AddModuleRow((int)module[0], (string)module[1], (string)module[2]);

            var userModuleData = dbAccess.ExecuteTableForCommand("Select * From TestUserModule");

            foreach (DataRow userModule in userModuleData.Rows)
                this.userAccessData.UserModule.AddUserModuleRow((int)userModule[0], (int)userModule[1]);
        }

        public bool Authorize(System.Security.Principal.IPrincipal principal, string context)
        {
            var result = from user in this.userAccessData.User
                         join userModule in this.userAccessData.UserModule on user.UserNo equals userModule.UserNo
                         join module in this.userAccessData.Module on userModule.ModuleNo equals module.ModuleNo
                         where user.UserName == principal.Identity.Name && module.ModuleName == context
                         select user;

            if (result.Any())
                return true;

            return false;
        }
    }

    [Description("A custom AuthorizationProvider class which authorizes users via information stored in a SqlServer Database.")]
    [DisplayName("SqlServerAuthorizationProvider")]
    public class SqlServerAuthorizationProviderData : AuthorizationProviderData
    {
        public SqlServerAuthorizationProviderData()
            : base("SqlServerAuthorizationProvider", typeof(SqlServerAuthorizationProvider))
        {
            this.Type = typeof(SqlServerAuthorizationProvider);
        }

        public SqlServerAuthorizationProviderData(String name, String connectionString)
            : base(name, typeof(SqlServerAuthorizationProvider))
        {
            this.connectionStringName = connectionString;
        }

        private const string connectionStringProperty = "connectionStringName";

        private String connectionStringName;

        [ConfigurationProperty(connectionStringProperty, IsRequired = true)]
        [Description("The name of the Database Instance to authorize users.")]
        [DisplayName("Database Instance")]
        [Reference(typeof(ConnectionStringSettingsCollection), typeof(ConnectionStringSettings))]
        public String ConnectionStringName
        {
            get
            {
                return (string)this[connectionStringProperty];
            }
            set
            {
                this[connectionStringProperty] = value;
            }
        }

protected override System.Linq.Expressions.Expression<Func<IAuthorizationProvider>> GetCreationExpression() { return base.GetCreationExpression(); } public override IEnumerable<Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration> GetRegistrations(IConfigurationSource configurationSource) { if (typeof(IAuthorizationProvider).IsAssignableFrom(this.Type) == false) { throw new ConfigurationErrorsException(String.Format("Exception thrown in {0}.GetRegistrations(...)", this.Type.Name)); } yield return new TypeRegistration<IAuthorizationProvider>(() => new SqlServerAuthorizationProvider(ConnectionStringName)) { Name = this.Name, Lifetime = TypeRegistrationLifetime.Transient }; } }

 

Anyone an idea what I'm doing wrong?

Feb 14, 2011 at 12:41 PM

Solved.

Didn't referenced the provider in application.