实施IPrincipal的问题

时间:2012-05-12 16:38:47

标签: asp.net-mvc iprincipal

尝试实现IPrincipal(ASP.NET MVC 3)并遇到问题: 我的自定义IPrincipal:

  interface IDealsPrincipal: IPrincipal
    {
        int UserId { get; set; }
        string Firstname { get; set; }
        string Lastname { get; set; }
    }


public class DealsPrincipal : IDealsPrincipal
    {

        public IIdentity Identity { get; private set; }
        public bool IsInRole(string role) { return false; }

        public DealsPrincipal(string email)
        {
            this.Identity = new GenericIdentity(email);
        }

        public int UserId { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }

    }

要序列化/反序列化,请使用以下类:

public class DealsPrincipalSerializeModel
    {
        public int UserId { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
    }

Application authenticate事件如下(工作正常!)

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                //get the forms ticket
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                //instantiate a new Deserializer
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                //deserialize the model
                DealsPrincipalSerializeModel serializeModel = serializer.Deserialize<DealsPrincipalSerializeModel>(authTicket.UserData);
                //put the values of the deserialized model into the HttpContext
                DealsPrincipal newUser = new DealsPrincipal(authTicket.Name); //this implements IPrincipal
                newUser.UserId = serializeModel.UserId;
                newUser.Firstname = serializeModel.Firstname;
                newUser.Lastname = serializeModel.Lastname;

                HttpContext.Current.User = newUser;                
            }
        }

正如您在上一个语句中所看到的,HttpContext被分配了这个新的DealsPrincipal(工作正常)。

问题是,如果想要在Controller(Action)中访问此用户,我总是会得到一个基类对象。如果我按如下方式投射用户:

User as DealsPrincipal 

获取UserId(示例:

( User as DealsPrincipal).UserId 

这总是空的!为什么?我错过了什么?

1 个答案:

答案 0 :(得分:0)

我需要调查更多以给出正确的答案,但看看这部分代码,它可以帮助你(WindowsAuthenticationModule.cs源代码的一部分)

void OnAuthenticate(WindowsAuthenticationEventArgs e) {
        //////////////////////////////////////////////////////////// 
        // If there are event handlers, invoke the handlers 
        if (_eventHandler != null)
             _eventHandler(this, e); 

        if (e.Context.User == null)
        {
            if (e.User != null) 
                e.Context.User = e.User;
            else  if (e.Identity == _anonymousIdentity) 
                e.Context.SetPrincipalNoDemand(_anonymousPrincipal, false /*needToSetNativePrincipal*/); 
            else
                e.Context.SetPrincipalNoDemand(new WindowsPrincipal(e.Identity), false /*needToSetNativePrincipal*/); 
        }
    }

在此代码中,我建议您在分配自定义IPrincipal实现的实例之前检查用户是否是匿名用户。此外,不确定此方法是在“protected void Application_AuthenticateRequest”之前还是之后执行。将尝试花更多时间对此进行调查。

另外,请看这篇文章:

http://msdn.microsoft.com/en-us/library/ff649210.aspx