2023-02-16 305
我有一个网站,内置在ASP.NET 3.5和SQL Server 2005中,使用SQL成员资格提供程序,并且可能是展示身份验证.
由于我的网站上的安全性需要很低,我想验证一次,然后保持登录无限期地持久(不给用户选择).
正在发生的事情是用户登录,留在会话中登录,然后在下次到达时,它们被退出.
如何将登录持续存在?
这是技术细节,我已经尝试过多种持续时间的变化.
Try
If Membership.ValidateUser(UserName.Text, Password.Text) Then
Security.UserManager.AuthenticateUser(UserName.Text)
If FormsAuthentication.GetRedirectUrl(UserName.Text, False) = "/default.aspx" Then
Try
'Custom Logic'
Catch Ex As Exception
'Custom Error handling'
End Try
Else
FormsAuthentication.RedirectFromLoginPage(UserName.Text, True)
End If
End If
Catch ex As Exception
RaiseEvent ExceptionThrown(New ApplicationException("An error occurred trying to log the user in after account creation.", ex))
End Try
Public Shared Sub AuthenticateUser(ByVal Username As String)
Dim Expiration As DateTime = DateTime.Now.AddMonths(3)
Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(Username, True, Expiration.Subtract(Expiration).TotalMinutes)
Dim EncryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
Dim AuthCookie As New HttpCookie(FormsAuthentication.FormsCookieName, EncryptedTicket)
AuthCookie.Expires = Expiration
HttpContext.Current.Response.Cookies.Add(AuthCookie)
End Sub
web配置:
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="GlobalConnString"
applicationName="/"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="0"
/>
</providers>
</membership>
<authentication mode="Forms">
<forms timeout="1439200" name="SITENAME" loginUrl="/login.aspx" />
</authentication>
编辑:以下是客户端存储的cookie信息:
Name ASP.NET_SessionId
Value r4dz1555f1pdne45n1zrlkmg
Host SITEADDRESS.com
Path /
Secure No
Expires At End Of Session
Name .ASPXAUTH
Value 648767AC72A60DBA49650A361A2FA446BA992F792055EF5B488CADC95DF495315C1C577F1C8E67E67BD937A7AB6CC5DAED85D8D64E4ED7867FC0FC395F48FED7FB631033CE441DE85223E8B3EBAE616C
Host www.SITEADDRESS.com
Path /
Secure No
Expires Tue, 09 Jun 2009 17:51:31 GMT
Name ASP.NET_SessionId
Value gn5pcymhfsnua455yp45wpej
Host www.SITEADDRESS.com
Path /
Secure No
Expires At End Of Session
Name SITENAME
Value 9610E8515F3DBC088DAC286E1F44311A20CB2BBB57C97F906F49BC878A6C6AC0B9011777402AEA130DCDC521EF4FBB3393DB310083F72EB502AE971183306C24F07F696B3695C67DD73166F1653DF52B
Host www.SITEADDRESS.com
Path /
Secure No
Expires Tue, 20 Dec 2011 06:14:10 GMT
我终于想到了最后一块拼图.当我的服务器的应用程序池正在回收(由托管提供程序配置)时,viewState加密密钥正在自动重新生成.这意味着即使cookie有效且未过期(预返回访问),当用户返回的endyration已更改时,Cookie不再有效.
解决方案是手动指定静态验证密钥.以下链接可用于为此生成Neccessary Web.config标记.
更新:
这是一个更可配置的站点来生成机器键
源树 – 生成属性
我意识到这可能有一个次要的安全影响,我猜测有一个更改的钥匙在你的钥匙被强制且妥协你在视图状态下存储的任何数据时,有更改的钥匙,但你可能不应该在ViewState中存储敏感信息,因为无论如何都没有固有的安全.
示例:
<configuration>
<system.web>
<machineKey
validationKey="97CEB2D3DEBF853649EAB851F56F08BA328423F935C97244CF5300925B6FF7D2C43084C73CBAF19D5193755EF7F87A3FFC714675F9197C822BAEEC97E853B91E"
decryptionKey="A72F69A4650348E3AA249A8179516D67C8553B3D4BD165B9"
validation="SHA1" />
</system.web>
</configuration>
我认为你可以使用FormsAuthentication.SetAuthCookie方法更好,而不是自己写很多代码.
我相信Web.config中的会员提供商设置可能与您提供的代码中提供的设置相冲突,而不是提供cookie名称.
尝试以下内容:
if (Membership.ValidateUser(userName, password))
{
FormsAuthentication.SetAuthCookie(userName, true); //Creates a cookie named "XXXAuth" - see settings in web.config below
}
与web.config中的以下设置一起使用:
<authentication mode="Forms">
<forms cookieless="UseCookies" loginUrl="~/SignIn.aspx" name="XXXAuth" slidingExpiration="true" timeout="432000"/>
</authentication>
<membership defaultProvider="XXXMembershipProvider">
<providers>
<clear />
<add name="XXXMembershipProvider" type="System.Web.Security.SqlMembershipProvider" applicationName="XXX" connectionStringName="XXX" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
只需更改身份验证块中的”超时”值,如果您真的想创建无限期的登录期限,则更长的值.我相信432000 = 5天.
如果您希望用户能够明确注销,只需响应按钮单击(或其他)来调用以下方法:
FormsAuthentication.SignOut();
读取您的代码,您可能会意外地将ForeSAuthenticationTicket超时值设置为零.您创建故障单的代码中的行: –
Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(Username, True, Expiration.Subtract(Expiration).TotalMinutes)
Expiration.Subtract(Expiration).TotalMinutes的值将始终为”0″.
您可能会使用冗长格式更好地创建票证,如下所示,这会导致较少的歧义.
Public Shared Sub AuthenticateUser(ByVal Username As String)
Dim Expiration As DateTime = DateTime.Now.AddMonths(3)
Dim userData As String = String.Empty
Dim authTicket As FormsAuthenticationTicket = _
New FormsAuthenticationTicket( _
Username, _
DateTime.Now, _
Expiration, _
true, _
userData, _
FormsAuthentication.FormsCookiePath)
Dim EncryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
Dim AuthCookie As New HttpCookie(FormsAuthentication.FormsCookieName, EncryptedTicket)
AuthCookie.Expires = Expiration
HttpContext.Current.Response.Cookies.Add(AuthCookie)
End Sub
还有一个很好的Microsoft KB文章这里在”了解表单上身份验证票证和cookie “
.
以上所述是小编给大家介绍的用ASP .Net会员制使用户登录持久化,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/33827.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态