You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET应用中链接无法跳转新页面且触发302错误的求助

解决GoDaddy部署ASP.NET应用后超链接触发302跳转登出的问题

1. 修正Forms Authentication Cookie配置

在web.config的<authentication>节点中,确保Cookie的域名、路径和安全属性适配GoDaddy环境,避免跨域或协议不一致导致会话失效:

<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx" 
         name=".ASPXAUTH" 
         timeout="2880"
         domain="yourdomain.com" <!-- 替换为你的实际域名 -->
         path="/"
         requireSSL="true" <!-- 站点启用HTTPS则设为true,否则为false -->
         slidingExpiration="true" />
</authentication>

2. 配置固定MachineKey

GoDaddy共享主机的应用池回收会自动重置验证密钥,导致会话验证失败。手动指定machineKey保证密钥稳定:

<system.web>
  <machineKey validationKey="生成的随机验证密钥" 
              decryptionKey="生成的随机解密密钥" 
              validation="SHA1" 
              decryption="AES" />
</system.web>

(可通过在线工具生成符合要求的随机密钥,部署后保持密钥不变)

3. 强制HTTPS请求(若站点启用HTTPS)

添加URL重写规则,确保所有请求统一走HTTPS,避免HTTP/HTTPS会话不一致引发的跳转:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Force HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

4. 调整Session状态配置

共享主机下InProc模式可能因应用池回收丢失会话,若使用该模式需确认超时设置合理;若允许,可切换至更稳定的StateServer模式:

<!-- InProc模式配置 -->
<sessionState mode="InProc" timeout="20" cookieless="false" />

<!-- 可选:StateServer模式(需确保StateServer服务运行) -->
<sessionState mode="StateServer" 
              stateConnectionString="tcpip=localhost:42424" 
              timeout="20" />

5. 修正LinkButton路径解析

将LinkButton的PostBackUrl改为客户端解析路径,避免服务器端路径解析异常:

<asp:linkbutton runat="server" 
                PostBackUrl="<%= Page.ResolveClientUrl("~/home.aspx") %>" 
                CssClass="btn btn-default btn-block" 
                style="text-align: center; color: white; background-color: darkgoldenrod" 
                Text="home"></asp:linkbutton>

6. 检查GoDaddy主机配置

  • 登录GoDaddy控制面板,确认应用程序池的.NET版本与本地开发环境一致;
  • 验证应用程序的虚拟目录路径设置正确,无层级冲突。

内容的提问来源于stack exchange,提问作者Kavya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:27:07