您的登录尝试未成功.请再试一次
本文关键字:一次 登录 未成功 | 更新日期: 2023-09-27 18:24:02
我在我的小型应用程序中使用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="data source=IP Address;initial catalog=DatabaseName;persist security info=True;user id=Username;password=password;MultipleActiveResultSets=True;App=EntityFramework"" 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.角色角色ID,角色名称2.用户[UserId]、[Username]、[Password]、[Email]、[CreatedDate]、[LastLoginDate]、[RoleId]、[Status]
请帮帮我……我不能理解确切的问题。
您有
if (!this.IsPostBack)
{
etc.
}
但你没有"其他…"(我想验证用户)。