我目前正在尝试取代我们用于所有内部网络应用程序的公司范围的用户身份验证,而不是我们当前的应用程序在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);
}
我创建cookie的方法
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;
}
}
解密cookie的方法
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类的源代码很有帮助,在这种情况下,我使用了: