自定义RoleProvider(继承自SqlRoleProvider)的问题
代码如下:
public class RoleProcess:SqlRoleProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
if (config == null)
{
throw new ArgumentNullException( "config ");
}
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException( "name ");
}
//璋冪敤鍩虹被
base.Initialize(name, config);
}
public override bool IsUserInRole(string username, string roleName)
{
if (base.IsUserInRole(username, roleName))
{
return true;
}
}
}
web.config如下(摘了部分)
<roleManager enabled= "true " defaultProvider= "customProvider ">
<providers>
<remove name= "AspNetSqlRoleProvider "/>
<add connectionStringName= "netSql " applicationName= "/ " name= "customProvider " type= "RoleProcess "/>
</providers>
</roleManager>
我写了段测试程序,来调用自定义RoleProcess提供的IsUserInRole,但是报异常了
异常提示如下:
用户代码未处理 System.NullReferenceException
Message= "
未将对象引用设置到对象的实例。 "
Source= "System.Web "
StackTrace:
在 System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation)
在 System.Web.Security.SqlRoleProvider.IsUserInRole(String username, String roleName)
在 RoleProcess.IsUserInRole(String username, String roleName) 位置 e:\cmd\App_Code\RoleProcess.cs:行号 58