我很难在具有自定义主体的MVC应用程序中实现“记住我”功能。我把它归结为ASP.NET,而不是为我检索身份验证cookie。我在Google Chrome中添加了以下快照。ASP.NET MVC身份验证Cookie未被检索(更新)
显示Request.Cookies的结果,这些结果在控制器操作中设置并放置在ViewData中供视图读取。请注意,它缺少.ASPXAUTH Cookie
显示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.com,http://www.mydomain.com和http://sub.mydomain.com)?
您可以发布您在用户登录过程中使用的代码吗?你只是使用内置的FormsAuthentication的东西,而不使用提供者?或者,您是否使用自己的自定义提供程序? – sestocker 2010-06-07 12:45:20
在初始登录期间,什么值与cookie关联(尤其是过期时间)? – sestocker 2010-06-07 13:30:22
以下是Cookie的样子:http://i50.tinypic.com/npm26c.png – 2010-06-07 13:48:58