简体   繁体   English

ASP.NET SqlMembershipProvider无限循环?

[英]ASP.NET SqlMembershipProvider Infinite Loop?

I am trying to configure authentication using a few tutorials I have found on the Membership Providers paradigm found in ASP.NET v2.0. 我正在尝试使用一些我在ASP.NET v2.0中的“成员资格提供程序”范例中找到的教程来配置身份验证。 I've followed the examples in the tutorial but can't seem to get the FormsAuthentication.RedirectFromPage method to work appropriately. 我已按照本教程中的示例进行操作,但似乎无法使FormsAuthentication.RedirectFromPage方法正常工作。 When I attempt a login, the user credentials are validated via Membership.ValidateUser but the page is sent back to Login.aspx instead of Default.aspx. 当我尝试登录时,将通过Membership.ValidateUser验证用户凭据,但该页面将发送回Login.aspx而不是Default.aspx。 Here is the relevant snippet from my web.config: 这是我的web.config中的相关代码段:

...
<authentication mode="Forms">
  <forms loginUrl="Login.aspx" protection="All" timeout="60" name="POTOKCookie" requireSSL="false" path="/FormsAuth"
         slidingExpiration="true" cookieless="UseCookies" enableCrossAppRedirects="false" defaultUrl="~/Default.aspx"/>
</authentication>
<authorization>
  <deny users="?" />
</authorization>
...
<membership defaultProvider="CustomizedProvider">
  <providers>
    <clear />
    <add name="CustomizedProvider"
         type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         connectionStringName="LoginDB2"
         applicationName="POTOK"
         minRequiredPasswordLength="5"
         minRequiredNonalphanumericCharacters="0" />
  </providers>
</membership>

I've verified that my connection string is correct (since Membership.ValidateUser seems to be working just fine) and am using the ASP.NET Login control for the UI on my Login.aspx page. 我已验证我的连接字符串正确(因为Membership.ValidateUser似乎工作正常),并且正在Login.aspx页上使用UI的ASP.NET登录控件。 Here is the authenticate event handler code: 这是验证事件处理程序代码:

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
    If (Membership.ValidateUser(Login1.UserName, Login1.Password)) Then
        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet)
    End If
End Sub

When I visit the url ( http://localhost/Project ) I am taken to: http://localhost/Project/Login.aspx and after the "login" my url is: http://localhost/Project/Login.aspx?ReturnUrl=%2fProject%2fDefault.aspx 当我访问URL( http:// localhost / Project )时,我被带到: http://localhost/Project/Login.aspx ,“登录”后,我的URL是: http:// localhost / Project / Login。 aspx?ReturnUrl =%2fProject%2fDefault.aspx

Did I miss a configuration step? 我错过了配置步骤吗?

The problem is in path="/FormsAuth" parameter. 问题出在path =“ / FormsAuth”参数中。 Remove this variable and try again 删除此变量,然后重试

Read this post about why path can be wrong 阅读这篇文章,了解为什么路径可能是错误的

From MSDN: path - Optional attribute. 从MSDN:path-可选属性。 Specifies the path for cookies that are issued by the application. 指定应用程序发布的cookie的路径。 The default is a slash (/), because most browsers are case-sensitive and will not send cookies back, if there is a path case mismatch. 默认值是斜杠(/),因为大多数浏览器区分大小写,并且如果路径大小写不匹配,则不会发送回cookie。

NOTE: The path attribute is case sensitive. 注意:path属性区分大小写。 Therefore, if the you set the value of the path attribute to /application1, and if the application name is Application1, the authentication cookie path is /application1. 因此,如果将path属性的值设置为/ application1,并且如果应用程序名称为Application1,则身份验证cookie路径为/ application1。

So if you want to use path property, you should set it to "/project" because Project is the name of your application (as far as I understood). 因此,如果要使用路径属性,则应将其设置为“ / project”,因为Project是应用程序的名称(据我了解)。 But I don't think you need to have different paths when you use different cookies names (ie name="POTOKCookie" in this application, i hope will be different from other ASP.NET applications installed on the same host) 但是,我认为您在使用不同的Cookie名称时不需要使用不同的路径(例如,在此应用程序中使用name =“ POTOKCookie”,希望与安装在同一主机上的其他ASP.NET应用程序有所不同)

See PRB: Forms Authentication Requests Are Not Directed to loginUrl Page 请参阅PRB:表单身份验证请求未定向到loginUrl页面

If you use the Login control with ASP.NET membership, you do not need to write code to perform authentication. 如果将登录控件与ASP.NET成员身份一起使用,则无需编写代码即可执行身份验证。 However, if you want to create your own authentication logic, you can handle the Login control's Authenticate event and add custom authentication code. 但是,如果要创建自己的身份验证逻辑,则可以处理Login控件的Authenticate事件并添加自定义身份验证代码。

So, I suggest you simply delete Login1_Authenticate event as far as it does the double work, I think, because control itself is responsible for calling ValidateUser and redirection. 因此,我认为您只要删除Login1_Authenticate事件就可以完成双重工作即可,因为控件本身负责调用ValidateUser和重定向。

Also check DestinationPageUrl property of the Login control 还要检查登录控件的DestinationPageUrl属性

If you do not specify a value for the DestinationPageUrl property, the user will be redirected to the original page the user requested after successfully logging in. So in your case this property should not be set. 如果未为DestinationPageUrl属性指定值,则在成功登录后,该用户将被重定向到用户请求的原始页面。因此,在这种情况下,不应设置此属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM