我遇到了一个奇怪的问题。
概要
我的登录页面可以针对SQL使用或AD用户进行身份验证。要确定用户是否为AD用户,用户名应包含反斜杠。
SQL的逻辑工作正常,但在针对AD验证用户时,我得到的是最一般的错误(Object reference not set to an instance of an object.
)。
详情
对于 SQL用户,我CustomSqlMembershipProvider()
。电话是这样的:
if(Membership.Provider.ValidateUser(userName, userPassword))
userAuthenticated = true;
类CustomSqlMembershipProvider()
将凭据发送到SQL数据库。
对于 AD用户,我有这样的逻辑:
if (Membership.Providers["ADMembership"].ValidateUser(userName, userPassword))
userAuthenticated = true;
以上if
语句生成错误Object reference not set to an instance of an object.
的Web.config
<add name="ConnectionStringAD" connectionString="LDAP://it.CompanyName.local" />
...
<membership defaultProvider="CustomSqlMembershipProvider" userIsOnlineTimeWindow="30">
<providers>
<clear/>
<add name="CustomSqlMembershipProvider"
type="Authentication.MembershipProviders.CustomSqlMembershipProvider"
connectionStringName="SqlProviderConnectionString"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/" />
<add name="ADMembership"
type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ConnectionStringAD"
connectionUsername="it\LDAPuser"
connectionPassword="LDAPuserPassword"
connectionProtection="Secure"
maxInvalidPasswordAttempts="5"
attributeMapUsername="sAMAccountName" />
</providers>
</membership>
我强烈认为存在一些小问题,但我无法理解。
软件
答案 0 :(得分:0)
这不是一个直接答案,但很少有建议缩小问题范围:
尝试在web.config中更改defaultProvider。将其设置为ADMembership。
打破电话:
if (Membership.Providers["ADMembership"].ValidateUser(userName, userPassword))
userAuthenticated = true;
到
var activeDirectoryProvider = Membership.Providers["ADMembership"];
if(activeDirectoryProvider != null)
{
userAuthenticated = true;
}
else
{
Log("activeDirectoryProvider is null");
}