2010-06-07 52 views
8

我很难在具有自定义主体的MVC应用程序中实现“记住我”功能。我把它归结为ASP.NET,而不是为我检索身份验证cookie。我在Google Chrome中添加了以下快照。ASP.NET MVC身份验证Cookie未被检索(更新)

  1. 显示Request.Cookies的结果,这些结果在控制器操作中设置并放置在ViewData中供视图读取。请注意,它缺少.ASPXAUTH Cookie

  2. 显示Chrome开发人员工具的结果。您可以看到.ASPXAUTH包含在这里。

alt text http://i50.tinypic.com/ibctjd.png

有谁知道什么问题可能在这里?为什么ASP.NET不从Cookie集合中读取此值?

我的应用程序使用自定义IPrincipal。 BusinessPrincipalBase是ust实现IPrincipal的CSLA对象。以下是该代码:

[Serializable()] 
public class MoralePrincipal : BusinessPrincipalBase 
{ 
    private User _user; 

    public User User 
    { 
     get 
     { 
      return _user; 
     } 
    } 

    private MoralePrincipal(IIdentity identity) : base(identity) 
    { 
     if (identity is User) 
     { 
      _user = (User)identity; 
     } 
    } 

    public override bool Equals(object obj) 
    { 
     MoralePrincipal principal = obj as MoralePrincipal; 
     if (principal != null) 
     { 
      if (principal.Identity is User && this.Identity is User) 
      { 
       return ((User)principal.Identity).Equals(((User)this.Identity)); 
      } 
     } 
     return base.Equals(obj); 
    } 

    public override int GetHashCode() 
    { 
     return base.GetHashCode(); 
    } 

    public static bool Login(string username, string password) 
    { 
     User identity = User.Fetch(username, password); 
     if (identity == null || !identity.IsAuthenticated) 
     { 
      identity = (User)User.UnauthenicatedIdentity; 
     } 

     MoralePrincipal principal = new MoralePrincipal(identity); 
     Csla.ApplicationContext.User = principal; 
     Context.Current.User = identity; 

     return identity != null && identity.IsAuthenticated; 
    } 

    public static void Logout() 
    { 
     IIdentity identity = User.UnauthenicatedIdentity; 
     MoralePrincipal principal = new MoralePrincipal(identity); 
     ApplicationContext.User = principal; 
     Context.Current.User = identity as User; 
    } 

    public override bool IsInRole(string role) 
    { 
     if (Context.Current.User == null || Context.Current.Project == null) 
     { 
      return false; 
     } 

     string userRole = Context.Current.User.GetRole(Context.Current.Project.Id); 
     return string.Compare(role, userRole, true) == 0; 
    } 

该应用程序还使用自定义成员资格提供程序。这是代码。

public class MoraleMembershipProvider : MembershipProvider 
{ 
    public override bool ValidateUser(string username, string password) 
    { 
     bool result = MoralePrincipal.Login(username, password); 
     HttpContext.Current.Session["CslaPrincipal"] = ApplicationContext.User; 
     return result; 
    } 

    #region Non-Implemented Properties/Methods 

    public override string ApplicationName 
    { 
     get 
     { 
      return "Morale"; 
     } 
     set 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    // Everything else just throws a NotImplementedException 

    #endregion 
} 

我不认为这是相关的,因为底线是Request.Cookies不返回认证Cookie。它与cookie的大小有关吗?我听说有关于cookie大小的问题。

更新:似乎问题围绕子域。这个网站是由一个子域名托管的,cookie域名保留空白。有没有人有关于如何让auth cookie与所有域一起工作的指示(例如http://mydomain.comhttp://www.mydomain.comhttp://sub.mydomain.com)?

+0

您可以发布您在用户登录过程中使用的代码吗?你只是使用内置的FormsAuthentication的东西,而不使用提供者?或者,您是否使用自己的自定义提供程序? – sestocker 2010-06-07 12:45:20

+0

在初始登录期间,什么值与cookie关联(尤其是过期时间)? – sestocker 2010-06-07 13:30:22

+0

以下是Cookie的样子:http://i50.tinypic.com/npm26c.png – 2010-06-07 13:48:58

回答

1

你也检查过吗?

ASPXAUTH cookie is not being saved

我不知道,如果这将是展示在Chrome浏览器cookie的原因,但实际上没有被传递给浏览器,或者如果它会阻止该Cookie从节约也值得一看。

+0

我致力于减小尺寸,并且仍然无法从Request.Cookies集合中获取cookie,除非在同一会话中。我知道这不是一个会话cookie,并且它被保存到磁盘。 – 2010-06-08 15:06:14

1

如果您试图将实际的用户对象存储在cookie本身中,则存储为cookie可能太大。我不是太熟悉与MVC认证的东西,但在Web表单我一般做到以下几点:

FormsAuthentication.RedirectFromLoginPage(user_unique_id_here, false); 

第二个参数是你正在寻找的持久性。

从那里我创建一个自定义上下文(UserContext),我通过HttpModule填充,使我可以访问所有用户和角色信息。

由于我没有在MVC(尚未)或CSLA中开发,我不知道我能有多少帮助。如果我是你,我也会抛弃自定义会员供应商。您可以直接在您的身份验证控制器中直接调用MoralePrincipal.Login。

+0

是的,这是我输入此信息后的想法。我认为我的用户对象的序列化版本太大了。我可能需要为IIdentity创建一个更小的可以安全序列化到磁盘的不同对象。 – 2010-06-07 20:30:20

+0

http://stackoverflow.com/questions/2224562/asp-net-formsa-authentication-redirect-loses-the-cookie-between-redirect-and-applic – 2010-06-07 20:31:38

+0

我曾使用一个较小的对象序列化,我仍然得到同样的问题。我再次陷入困境。 – 2010-06-08 15:04:57

1

如果您使用的是“常规”AccountController代码,那么rememberMe的东西应该由MVC1中的FormsAuthenticationService(或MVC2中的)或FormsAuthentication静态类设置。如果更改了代码,是否记得添加(可选)布尔参数以指示是否使用持久cookie?

这听起来像你得到一个会话cookie,但不是一个持久性cookie。

+0

Cookie存在于浏览器的Cookie中。我已经使用Firefox中的Firebug和Chrome中的开发者工具对此进行了验证。问题是.NET只是不会返回集合中的cookie。这不是一个会话cookie。 – 2010-06-21 01:14:03