我正在嘗試檢查給定用戶在完全「登錄」到我正在構建的站點之前是否有角色。通常,我會使用代碼:鑑於UserName的字符串,我如何檢查用戶IsInRole?
User.IsInRole("CustomRole")
但在這種情況下,該行始終得到「假」 - 我相信這是因爲使用User.IsInRole,用戶必須已經完全記錄我在嘗試。在我的賬戶控制器的LogOn方法中檢查這條信息,所以用戶還沒有登錄(我認爲)。
我怎麼會返回一個用戶對象,這樣我可以做什麼,我嘗試下面的事情:
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
// Need to identify the user because the "User" is not officially 'logged in' yet and cannot be accessed via "User.IsInRole" - am I correct in this understanding
MembershipUser u = Membership.GetUser(model.UserName);
if (u.IsInRole("Administrator"))
. . . . truncated
上面的代碼引發以下錯誤:
'System.Web.Security.MembershipUser' does not contain a definition for 'IsInRole' and no extension method 'IsInRole' accepting a first argument of type 'System.Web.Security.MembershipUser' could be found (are you missing a using directive or an assembly reference?)
的MembershipUser U =會員.GetUser(model.UserName)顯然沒有返回一個對象,我可以使用IsInRole,任何提示?