WebViewPage的MVC IPrincipal用户为空

时间:2016-02-02 16:19:08

标签: asp.net-mvc authentication model-view-controller

我为我的视图创建了一个基类,如下所示:

public abstract class BaseViewPage : WebViewPage
{
    public virtual new CustomPrincipal User
    {
        get
        {
            if (base.User == null) return null;
            return base.User as CustomPrincipal;
        }
    }
}

public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
{
    public virtual new CustomPrincipal User
    {
        get
        {
            if (base.User == null) return null;
            return base.User as CustomPrincipal;
        }
    }

    public override void Execute()
    {
        throw new NotImplementedException();
    }
}

在我的模型中我有:

public class SecureAreaModel : BaseViewPage
{
    public int MyUserID
    {
        get { return User.ID; }
        private set { }
    }

    public SecureAreaModel(ControllerContext controllerContext)
    {

    }

    public override void Execute()
    {
        throw new NotImplementedException();
    }
}

我想使用属性MyUserID但我收到此错误: Error

此时用户是认可的

    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 customer = new CustomPrincipal(serializeModel.Email);
            customer.ID = serializeModel.ID;
            customer.Email = serializeModel.Email;
            customer.FirstName = serializeModel.FirstName;
            customer.LastName = serializeModel.LastName;
            customer.Roles = serializeModel.Roles;

            HttpContext.Current.User = customer;
        }
        else
        { 
            HttpContext.Current.User = new CustomPrincipal(string.Empty);
        }
    }

任何帮助将不胜感激! THX

0 个答案:

没有答案