表单身份验证在本地工作,但不在开发服务器上



我知道Forms Authentication是旧的,但是当我使用IIS Express在本地运行web应用程序时,一切都工作得很好。但是当我将它发布到我们的开发/测试服务器时,它只是重新加载页面。

还有一点需要注意,它在本地运行为localhost:50264/Login。在开发服务器上,url更像是http://dev1.server.com/op/webapp/Account/Login.

我注意到两个cookie的路径都是"/"。我确实试图通过在我的本地网站上设置这个来改变它。配置:

<add key="CookiePath" value="/" /> 

然后当我发布到我们的开发服务器时它变成了:

<add key="CookiePath" value="http://dev1.server.com/op/webapp/" xdt:Transform="Replace" xdt:Locator="Match(key)" />

这似乎行不通。

在另一个线程,我发现在堆栈溢出,有人建议将此添加到:

<system.webServer>
    <modules>
        <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
    </modules>
</system.webServer>

那也没用。任何帮助将非常感激!

更新:9/29/2016

我删除了CookiePath应用程序设置,而是对身份验证节点进行了调整。在我的网上。我现在有:

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" requireSSL="false" slidingExpiration="true" path="/" />
</authentication>

和在我的Web.Debug.config我有:

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" requireSSL="false" slidingExpiration="true" path="/op" xdt:Transform="Replace" />
</authentication>

最后,当我创建cookie时:

var authTicket = new FormsAuthenticationTicket(
    1,
    user.Email,
    DateTime.Now,
    DateTime.Now.AddDays(14),
    true,
    userData,
    FormsAuthentication.FormsCookiePath);

当我部署到开发服务器时,我检查web。在那里配置,它确实正确地转换了表单节点。

当我登录时,我输入我的凭据,它仍然刷新登录页面。使用Chrome扩展"EditThisCookie",我仍然看到cookie的路径是"/"。它根本无法识别变化。即使我手动将authTicket路径的路径设置为"/op",cookie的路径仍然为"/"。我不知道发生了什么。呃…

我也使用表单身份验证,这是我的设置。您没有显示所有表单的身份验证代码,但希望这将为您指明正确的方向。

网络。配置

<authentication mode="Forms">
  <forms loginUrl="members/login.aspx" name=".ASPXFORMSAUTH" requireSSL="false" slidingExpiration="true" timeout="120" />
</authentication>

然后在用户登录时在代码中设置cookie。

  Dim authCookie As HttpCookie = FormsAuthentication.GetAuthCookie(iMembersID, False)
  Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
  Dim newTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, "Member")
  authCookie.Value = FormsAuthentication.Encrypt(newTicket)
  Response.Cookies.Add(authCookie)

然后测试它们是否在需要用户登录的所有页面上进行了身份验证。

    If Request.IsAuthenticated Then
        Dim ident As FormsIdentity = CType(User.Identity, FormsIdentity)
        If ident IsNot Nothing Then
            Dim ticket As FormsAuthenticationTicket = ident.Ticket
            Dim userDataString As String = ticket.UserData
            Select Case ticket.UserData
                Case "Member"
                    m_MemberLoggedIn = ident.Name
                Case Else
                    Response.Redirect("~/members/login/", True)
            End Select

        Else
            Response.Redirect("~/members/login/", True)
        End If

更新9/29:

检查确保IIS身份验证模式设置为匿名

我采取了简单的方法,让我们的IT部门创建一个子域名,这样cookie的路径将始终是"/"。这不是答案,但我就是这么做的。

最新更新