ASP.NET MVC身份验证Cookie未被检索

8
我在使用自定义主体的MVC应用程序中实现“记住我”功能时遇到了困难。我已经将问题归结为ASP.NET未检索到我的身份验证cookie。下面是来自Google Chrome的快照:
  1. 显示了在控制器操作中设置并放置在ViewData中供视图读取的Request.Cookies的结果。请注意,.ASPXAUTH cookie丢失了。

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

alt text

这里可能有什么问题?为什么ASP.NET不能从cookie集合中读取该值?
我的应用程序使用自定义IPrincipal。BusinessPrincipalBase是一个实现了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://example.comhttp://www.example.comhttp://sub.example.com)中工作吗?

你能发布一下在用户登录过程中所使用的代码吗?你是只使用了内置的FormsAuthentication功能,没有使用提供程序吗?还是你使用了自己的自定义提供程序? - sestocker
在初始登录期间,与Cookie相关联的值是什么(特别是过期时间)? - sestocker
这是 cookie 的样子:http://i50.tinypic.com/npm26c.png - Jamie Wright
2个回答

1

如果您试图将实际的用户对象存储在cookie中,那么它可能太大了无法作为cookie存储。虽然我对MVC身份验证不是非常熟悉,但通常在Web表单中,我会执行以下操作:

FormsAuthentication.RedirectFromLoginPage(user_unique_id_here, false);

第二个参数是您要寻找的持久性。

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

由于我还没有在 MVC 或 CSLA 中开发,所以我不确定我能提供多少帮助。如果我是你,我也会放弃自定义成员资格提供程序。您可能也可以直接在身份验证控制器中调用 MoralePrincipal.Login。


是的,在我打出这段代码后,这也是我的想法。我认为我的用户对象的序列化版本太大了。我可能需要创建一个不同的IIdentity对象,它更小并且可以安全地序列化到磁盘上。 - Jamie Wright
https://dev59.com/DUvSa4cB1Zd3GeqPhtmC - Jamie Wright
我尝试使用一个较小的序列化对象,但仍然遇到了相同的问题。我又被卡住了。 - Jamie Wright

1

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

听起来像是您正在获取会话cookie,但没有获取到持久性cookie。


Cookie存在于浏览器的cookies中。我已经使用Firefox中的Firebug和Chrome中的Developers Tools进行了验证。问题在于.NET只是没有在集合中返回cookie。它不是会话cookie。 - Jamie Wright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接