我已按照此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