我已按照此guide向IPrincipal添加自定義特性。無法將類型'CustomPrincipal'轉換爲'System.Security.Principal.IPrincipal'
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
JavaScriptSerializer serializer = new JavaScriptSerializer();
CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);
CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
newUser.UserId = serializeModel.UserId;
newUser.FirstName = serializeModel.FirstName;
newUser.LastName = serializeModel.LastName;
HttpContext.Current.User = newUser; /This line makes an error.
}
}
這是最後一行發生錯誤。
Cannot implicitly convert type 'SocialMedia.Models.CustomPrincipal' to 'System.Security.Principal.IPrincipal'. An explicit conversion exists (are you missing a cast?)
可能是爲什麼它不起作用的問題。
如何聲明'CustomPrincipal'類? – Sakura