1

在我的 default.aspx 上是一个登录页面:

默认.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Portal.Default"
    MasterPageFile="Portal.Master" Title="Login" %>

<asp:Content runat="server" ContentPlaceHolderID="MainContentPlaceHolder">
    <div class="block" id="login">
        <h2>Login</h2>
        <asp:Label style="top: 15px;left: 11px" runat="server" Text="User Name :" CssClass="label" />
        <asp:TextBox ID="txtLoginUserName" Style="top: 15px;left: 17px; width:100px" runat="server"  CssClass="textbox" />
        <asp:Label style="top: 20px;left: 19px;" runat="server" Text="Password :" CssClass="label" />
        <asp:TextBox ID="txtLoginPassword" Style="top: 20px;left: 25px; width:100px;"  runat="server" TextMode="Password" CssClass="textbox" />
        <asp:Button ID="btnLogin" Style="top:30px;left: 91px" CssClass="button" runat="server" Text="Login" OnClick="BtnSubmitClick" />
    </div>
</asp:Content>

后面的代码:

using System;
using System.Linq;
using System.Web.Security;

namespace Portal
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Form.DefaultFocus = txtLoginUserName.ClientID;
        }

        protected void BtnSubmitClick(object sender, EventArgs e)
        {
            using (var db = new DbContext())
            {
                if (db.Users.Any(x => x.Username == txtLoginUserName.Text && x.Password == txtLoginPassword.Text))
                {
                    FormsAuthentication.RedirectFromLoginPage(txtLoginUserName.Text, true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "myalert", "alert('* invalid credentials, please try again');", true);
                }
            }
        }
    }
}

网络配置:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>

  <location path="default.aspx">
    <system.web>
      <authorization>
        <allow users="?" />
      </authorization>
    </system.web>
  </location>

  <location path="Styles">
    <system.web>
      <authorization>
        <allow users="?" />
      </authorization>
    </system.web>
  </location>

  <location path="Console.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

  <location path="Options.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

  <system.web>

    <authorization>
      <deny users="?" />
    </authorization>

    <authentication mode="Forms">
      <forms name="Portal" loginUrl="default.aspx" defaultUrl="console.aspx" />
    </authentication>

    ...

  </system.web>

</configuration>

问题是当我访问我的网站时,http://www.myhostname.com/default.aspx它没有带我去http://myhostname.com/default.aspx?ReturnUrl=%2f哪个奇怪的地方,所以我输入用户名和密码,点击登录...一切都变成空白...网址更改为http://www.myhostname.com,我输入所有内容第二次...之后它登录...并转到console.aspx

为什么这样做?

4

2 回答 2

0

如指定here

http://msdn.microsoft.com/en-US/library/s09bfdt2(v=vs.80).aspx

FormsAuthentication.RedirectFromLoginPage

将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

ReturnUrl=%2f

用于您的第一次登录和第二次的默认网址

于 2012-12-20T13:40:56.847 回答
0

我这样解决了这个问题:

  1. 将 default.aspx 重命名为 login.aspx 并更新了所有参考。
  2. 将 console.aspx 重命名为 default.aspx 并更新了所有参考。

现在我将 web.config 更改为看起来更简单,如下所示:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>

    <authorization>
      <deny users="?"/>
    </authorization>

    <authentication mode="Forms">
    </authentication>

    ...

  </system.web>  
</configuration>

现在它可以在第一次登录时使用,我对所有这些简化都很满意:D

于 2012-12-20T14:49:08.213 回答