2012-07-11 61 views
0

我想要一個System.Web.Security.MembershipUser用戶,所以我可以檢查它的lastPasswordChangedDate屬性,只有問題是它返回null。Membership.GetUser()// GetUser(userName)返回一個空用戶

我已經看過,大部分類似的問題似乎是因爲用戶在嘗試創建對象時未經過身份驗證。 在我的情況下,用戶在調用方法之前直接進行身份驗證,儘管在另一個類中。

任何指針?

僞代碼如下:

前端----> 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 
} 
+0

你能告訴我們你的代碼嗎? – user7116 2012-07-11 14:20:03

+0

這是相當多的,但我認爲這是必要的 – Dani 2012-07-11 14:40:23

回答

0

嘗試這樣的事情。 創建一種方法來獲取用戶

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