0

我正在尝试获取 System.Web.Security.MembershipUser 用户,以便检查它的 lastPasswordChangedDate 属性,唯一的问题是它返回 null。

我环顾四周,大多数类似的问题似乎是因为用户在尝试创建对象时没有经过身份验证。在我的情况下,用户在调用该方法之前直接进行了身份验证,尽管是在另一个类中。

任何指针?

伪代码如下:

前端----> Login.cs

string userName = txtUser.Text; string password = txtPW.Text;
wsAuth = new wsAuth();
wsAuthentication.Authenticate(userName,password);

认证服务---->

public bool Authenticate(string userName, string password) 
{
SecurityProviderSettings settings = SecurityProviderSettings.GetFromConfiguration("SecurityProviders");
ProviderSetting providerSetting = settings.Providers[providerName];
object provider = Activator.CreateInstance(providerType);
return ((IAuthenticationProvider)provider).Authenticate(userName, password, providerSetting.CustomInfo, out failReason, out token);
}

特定提供者---->

public bool Authenticate(string userName, string password, out string failReason .....)
{
SqlMembershipProvider provider = new SqlMembershipProvider();

#region settings
//blablabla
#endregion 

provider.initialize(/*input vars*/);
bool authenticated = provider.ValidateUser(userName,password);
if(authenticated)
{
 pwValidator val = new pwValidator();
  if(!val.IsInLifetime(userName,password))
   {
     failReason = "password expired";
   }

}
}

密码验证器---->

 public bool IsInLifetime(string userName, string password)
 {
MembershipUser user = Membership.GetUser(userName);
MembershipUser user2 = Membership.GetUser();
#bla bla bla
 }
4

1 回答 1

0

尝试这样的事情。创建获取用户的方法

        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
        {
            return Membership.GetUser();
        }
于 2012-07-11T14:24:58.143 回答