【问题标题】:Problems implementing IPrincipal实施 IPrincipal 的问题
【发布时间】:2012-05-12 16:38:46
【问题描述】:

尝试实现 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; }
    }

应用程序身份验证事件如下(工作正常!)

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(工作正常)。

问题是如果想在控制器(动作)中访问这个用户,我总是得到一个基类对象。如果我按如下方式投射用户:

User as DealsPrincipal 

获取例如 UserId(示例:

( User as DealsPrincipal).UserId 

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

【问题讨论】:

    标签: asp.net-mvc iprincipal


    【解决方案1】:

    我需要进行更多调查才能给你正确的答案,但看看这部分代码,它可以帮助你(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

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多