Мне трудно реализовать функцию "Запомнить меня" в приложении MVC с пользовательским принципалом. Я свел его к ASP.NET, не получая cookie для аутентификации для меня. Я включил снимок ниже из Google Chrome.
Показывает результаты Request.Cookies, которые установлены в действии контроллера и помещены в ViewData для представления для чтения. Обратите внимание, что в нем отсутствует файл cookie .ASPXAUTH
Показывает результаты инструментов разработчика Chrome. Вы можете видеть, что .ASPXAUTH включен сюда.
альтернативный текст http://i50.tinypic.com/ibctjd.png
В чем здесь проблема? Почему ASP.NET не читает это значение из коллекции файлов cookie?
Мое приложение использует собственный IPrincipal. BusinessPrincipalBase - это объект CSLA, который реализует IPrincipal. Вот код для этого:
[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 для работы работать со всеми доменами (например, http://example.com, http://www.example.com, и http://sub.example.com)?