asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net – 通过ASP .Net成员身份进行用户登录大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个建立在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 datetiR_267_11845@e = datetiR_267_11845@e.Now.AddMonths(3)

    Dim authTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(Username,True,Expiration.Subtract(Expiration).@R_56_10586@lminutes)
    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

解决方法

我认为你会更好地使用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>

如果您真的想创建一个无限期的登录期,只需将身份验证块中的“timeout”值更改为更长的值即可。我相信432000 = 5天。

如果您希望用户能够显式注销,只需调用以下方法来响应按钮单击(或任何):

FormsAuthentication.SignOut();

希望这可以帮助。

大佬总结

以上是大佬教程为你收集整理的asp.net – 通过ASP .Net成员身份进行用户登录全部内容,希望文章能够帮你解决asp.net – 通过ASP .Net成员身份进行用户登录所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。