簡體   English   中英

MVC4自定義表單身份驗證-無法將System.Web.HttpContext.Current.User強制轉換為CustomPrincipal

[英]MVC4 Custom Forms Authentication - Unable to cast System.Web.HttpContext.Current.User as CustomPrincipal

我正在.Net MVC4中建立網站,並嘗試使用CustomPrincipal對象實施自己的表單身份驗證。

這是我的登錄方法...

public void Login(string email)
{
    var user = _userManager.GetUserByEmail(email);

    var encryptedCookieContent = FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
        false, user.Id + "|" + user.AccountId + "|" + user.RoleId + "|" + user.Role.Name, FormsAuthentication.FormsCookiePath));

    var formsAuthenticationTicketCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookieContent)
    {
        Domain = FormsAuthentication.CookieDomain,
        Path = FormsAuthentication.FormsCookiePath,
        HttpOnly = true,
        Secure = FormsAuthentication.RequireSSL
    };

    HttpContext.Current.Response.Cookies.Add(formsAuthenticationTicketCookie);
}

這是我的自定義校長

public class CustomPrincipal : IPrincipal
{
    public CustomPrincipal(IIdentity identity, int userId, int accountId, string role, int roleId)
    {
        Identity = identity;
        Role = role;
        RoleId = roleId;
        UserId = userId;
        AccountId = accountId;
    }

    public bool IsInRole(string role)
    {
        return Role == role;
    }

    public bool IsInRole(int roleId)
    {
        return RoleId == roleId;
    }

    public IIdentity Identity { get; private set; }

    public string Role { get; private set; }
    public int RoleId { get; private set; }

    public int UserId { get; private set; }
    public int AccountId { get; private set; }
}

我沒有實現Identity的自定義實例,而是在重用GenericIdentity。 在我的Global.asax文件中,我像這樣安裝了Application_AuthenticateRequest方法...

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    if (Request.IsAuthenticated)
    {
        var formsCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (formsCookie != null)
        {
            var ticket = FormsAuthentication.Decrypt(formsCookie.Value);
            var principal = PrincipalConfig.CreatePrincipal(ticket);
            System.Web.HttpContext.Current.User = Thread.CurrentPrincipal = principal;
        }
    }
}

PrincipalConfig.CreatePrincipal是將我的自定義主體對象放在一起的方法。 這基本上只是填寫userData ...這樣的...

public static CustomPrincipal CreatePrincipal(FormsAuthenticationTicket ticket)
        {
            var userData =  ticket.UserData.Split('|');
            var principal = new CustomPrincipal(new GenericIdentity(ticket.Name), Convert.ToInt32(userData[0]), Convert.ToInt32(userData[1]), userData[3], Convert.ToInt32(userData[2]));
            return principal;
        }

因此,現在回頭參考global.asax文件中的Application_AuthenticateRequest,您可以看到我正在使用新創建的CustomPrincipal對象並將其分配給System.Web.HttpContext.Current.User。

這樣做之后,如果我仍然在Application_AuthenticateRequest方法中,但在即時窗口中放置了以下內容...

System.Web.HttpContext.Current.User as CustomPrincipal

就像我期望的那樣,向我顯示了CustomPrincipal對象中的所有數據。

但是,如果稍后(在其他一些控制器操作中)嘗試訪問System.Web.HttpContext.Current.User並將其強制轉換為CustomPrincipal,它將返回null。

再次,對於應用程序中發出的每個請求,我都可以從Application_AuthenticateRequest方法內部的FormsAuthentication cookie解密FormsAuthenticationTicket,然后找到我的所有UserData。 因此,似乎cookie已被創建並正確讀取。 但是,當將新的CustomPrincipal分配給System.Web.HttpContext.Current.User時,從那時到我嘗試在另一個控制器內再次訪問它時,似乎存在某種斷開連接。

有人可以在這里看到我做錯了嗎?

讓我知道是否需要澄清任何事情。

謝謝

我不確定你在用

Request.IsAuthenticated

正確。

Request.IsAuthenticated屬性本質上返回與Context.User.Identity.IsAuthenticated屬性相同的值。 在您的代碼中,它應始終返回false,因此分配自定義主體的代碼將永遠不會觸發。

您可以通過在控制器方法中設置斷點並檢查代碼中Context.Current.User的類型來對其進行調試。 是GenericIdentity嗎? 如果是這樣,則永遠不會分配您的校長。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM