gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:45:41 24 4
gpt4 key购买 nike

我很难在具有自定义主体的 MVC 应用程序中实现“记住我”功能。我将其归结为 ASP.NET 没有为我检索身份验证 cookie。我在下面添加了 Google Chrome 的快照。

  1. 显示在 Controller 操作中设置并放置在 ViewData 中以供 View 读取的 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;
}

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

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 域留空。有谁知道如何让身份验证 cookie 与所有域一起使用(例如 http://example.comhttp://www.example.comhttp://sub.example.com )?

最佳答案

如果您尝试将实际的 User 对象存储在 cookie 本身中,它可能太大而无法存储为 cookie。我不太熟悉 MVC 身份验证内容,但在 Web 表单中我通常会执行以下操作:

FormsAuthentication.RedirectFromLoginPage(user_unique_id_here, false);

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

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

由于我还没有使用 MVC 或 CSLA 进行开发,所以我不确定我还能提供多少帮助。如果我是你,我也会放弃定制成员(member)服务提供商。您也可以直接在身份验证 Controller 中调用 MoralePrincipal.Login。

关于ASP.NET MVC 身份验证 Cookie 未检索到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2989099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com