您的登录尝试未成功。请再试一次

时间:2014-09-22 07:08:29

标签: c# asp.net login asp.net-web-api login-control

我在我的小应用程序中使用Formsauthentication进行登录。它在我的本地计算机上工作正常但是当我在云托管上传它时会出现错误'您的登录尝试未成功。请再试一次。'消息。

我的login.aspx.cs文件包含以下代码

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (this.Page.User.Identity.IsAuthenticated)
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/Login.aspx");
                }
            }
        }

        protected void ValidateUser(object sender, EventArgs e)
        {
            try
            {
                int userId = 0;
                string roles = string.Empty;
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("Validate_User"))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Username", Login1.UserName);
                        cmd.Parameters.AddWithValue("@Password", Login1.Password);
                        cmd.Connection = con;
                        con.Open();
                        SqlDataReader reader = cmd.ExecuteReader();
                        reader.Read();
                        userId = Convert.ToInt32(reader["UserId"]);
                        roles = reader["Roles"].ToString();
                        con.Close();
                    }
                    switch (userId)
                    {
                        case -1:
                            Login1.FailureText = "Username and/or password is incorrect.";
                            break;
                        case -2:
                            Login1.FailureText = "Account has not been activated.";
                            break;
                        default:
                            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Login1.UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), Login1.RememberMeSet, roles, FormsAuthentication.FormsCookiePath);
                            string hash = FormsAuthentication.Encrypt(ticket);
                            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

                            if (ticket.IsPersistent)
                            {
                                cookie.Expires = ticket.Expiration;
                            }
                            Response.Cookies.Add(cookie);
                            Response.Redirect(FormsAuthentication.GetRedirectUrl(Login1.UserName, Login1.RememberMeSet));
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = "Ehh.. Enter correct password..";
            }
        }

和我的web.config文件有代码

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="constr" connectionString="Data Source=IP Address;Initial Catalog=DatabaseName;user id=Username;password=PWD;" />
    <add name="ECOLLEGEMastersEntities" connectionString="metadata=res://*/MastersDataModel1.csdl|res://*/MastersDataModel1.ssdl|res://*/MastersDataModel1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=IP Address;initial catalog=DatabaseName;persist security info=True;user id=Username;password=password;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <machineKey validationKey="24389C4FE31C25B571C6E93CBAD77C8487C66B37FF1E972D392BCC7C58A1412791CD25CA0CC8922E6F88A6B20529F26F0C3743F82E902D7C9C9BBF536B2B02C2" 
                decryptionKey="05F5BCA7FF367A1C7EC9C2E5CF898B37E445810ACB0F4E3125144321C733E93C" validation="SHA1" decryption="AES" />
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms defaultUrl="~/Home.aspx" loginUrl="~/Login.aspx" slidingExpiration="true" timeout="2880" />
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
    <siteMap enabled="true" defaultProvider="SiteMap">
      <providers>
        <add name="SiteMap" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Web.sitemap" securityTrimmingEnabled="true" />
      </providers>
    </siteMap>
    <pages>
      <controls>
        <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
      </controls>
    </pages>
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

我的数据库表有2个表 1.角色 RoleID,RoleName 2.用户 [用户ID],[用户名],[密码],[电子邮件],[CreatedDate],[LastLoginDate],[角色ID],[状态]

请帮助我......我无法理解确切的问题。

1 个答案:

答案 0 :(得分:0)

你有

if (!this.IsPostBack)
{
    etc.
}

但是你没有&#39;否则......&#39; (验证用户,我想)。