Membership.GetUser()// GetUser(userName)返回null用户

时间:2012-07-11 14:18:28

标签: c# asp.net security

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

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

任何指针?

PSEUDO-CODE跟随:

FrontEnd ---->     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";
   }

}
}

PasswordValidator ---->

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

1 个答案:

答案 0 :(得分:0)

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

        HttpContext httpContext = HttpContext.Current;
        if (httpContext != null && httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
        {
            return Membership.GetUser();
        }