System.ArgumentException:在第一次成功登录后,'encryptedTicket'参数的值发生在任何登录上



我目前正在尝试替换我们公司范围的用户身份验证,我们用于所有内部 Web 应用程序,而不是我们当前的身份验证是在 2006 年制作的,并且常规失败。我被告知要尽可能简单地在所有现有项目上实施。它是一个 .NET 类库。它.dll将作为对现有项目的引用添加。

我遇到了一个问题,我可以在所有 cookie 被清除后登录一次。一旦我注销并重新登录,我就会发现System.ArgumentException: Invalid value for 'encryptedTicket' parameter.我发现一些帖子表明cookie可能为空,或者我没有尝试解密名称而不是值,但事实并非如此。这发生在铬和边缘上。

但是,每次都会对用户进行身份验证,假设在我被重定向到成功页面时使用正确的用户名和密码。

身份验证后,我添加一个cookie,然后重定向。

private void AddCookie(int compID, bool persist, HttpContext httpContext)
{
httpContext.Request.Cookies.Add(SetUpSession(compID, persist));
FormsAuthentication.RedirectFromLoginPage(compID.ToString(), persist);
}

我创建饼干的方法

private HttpCookie SetUpSession(int companyID, bool persist)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,                                     // ticket version
companyID.ToString(),                  // authenticated username
DateTime.Now,                          // issueDate
DateTime.Now.AddMinutes(30),           // expiryDate
persist,                               // true to persist across browser sessions                                                    
FormsAuthentication.FormsCookiePath);  // the path for the cookie
String encTick = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie("Cookie", encTick);
cookie.HttpOnly = true;
return cookie;
}

重定向到成功页面后,有一个截图代码,用于检查用户是否已登录。这是错误发生的地方

public dynamic isLoggedIn(HttpContext httpContext)
{
AuthenticationUtilities authUtil = new AuthenticationUtilities();
if (httpContext.Response.Cookies["Cookie"] != null)
{
companyID = authUtil.Authenticate(httpContext.Request.Cookies["Cookie"]);//the error occurs here
authUtil = new AuthenticationUtilities(companyID);
return authUtil;
}
else
{
httpContext.Response.Redirect("~/login.aspx");
return null;
}
}

解密饼干的方法

public int Authenticate(HttpCookie cookie)
{
FormsAuthenticationTicket authTick = FormsAuthentication.Decrypt(cookie.Value);
return int.Parse(authTick.Name);
}

在需要用户登录的任何页面上调用此方法,如下所示。

LMFJAuth.AuthenticationUtilities auth = _LMFJAuth.isLoggedIn(HttpContext.Current);//if the cookie is null it redirects to login. 

这是注销方法

public void LogOut(HttpContext httpContext)
{
FormsAuthentication.SignOut();
HttpCookie cookie = new HttpCookie("Cookie");
cookie.Expires = DateTime.Now.AddMinutes(-1);
httpContext.Session.Clear();
httpContext.Response.Cookies.Add(cookie);
httpContext.Response.Redirect(FormsAuthentication.LoginUrl);
}

somone 能否帮助解释在第一次成功登录/注销后加密勾选的值显示为无效的情况?

对我来说,这是cookie的加密值。值即将大于最大值 4096,在我的例子中为 4200。我刚刚向用户数据添加了一些角色字符串。

当我遇到困难时,我发现查找Microsoft类的源代码很有帮助,在这种情况下,我使用了:

http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/xsp/System/Web/Security/FormsAuthentication@cs/1/FormsAuthentication@cs。

最新更新