我的ASP.net網站遇到了一些麻煩;ASP.Net登錄身份驗證
當我轉到我的網站我可以登錄,沒問題。但是,當我轉到頁面的第一個地址時,我可以繞過我的登錄。
www.123.com < - 登錄罰款指引我 - > www.123.com/Memebers/members.aspx但如果我直接去www.123.com/Memebers/members。 aspx我可以完全繞過登錄。
什麼,我希望它做的是重定向到登錄頁面,如果有人試圖去直接聯繫完全錯過了登錄,我可以看到這是非常不安全的
這裏是我的LoginPage代碼;
<asp:Login ID="LoginControl" runat="server"
OnAuthenticate="LoginControl_Authenticate">
<asp:TextBox Placeholder="UserName" ID="UserName" runat="server" OnTextChanged="UserName_TextChanged" CssClass="input">
</asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="LoginControl">
</asp:RequiredFieldValidator>
<asp:TextBox Placeholder="Password" ID="Password" runat="server" TextMode="Password" CssClass="input">
</asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="LoginControl">
</asp:RequiredFieldValidator>
<asp:Literal ID="FailureText" runat="server" EnableViewState="False">
</asp:Literal>
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginControl" CssClass="Lbutton" />
</asp:Login>
後端LoginPage:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginControl_Authenticate(object sender, AuthenticateEventArgs e)
{
bool authenticated = this.ValidateCredentials(LoginControl.UserName, LoginControl.Password);
if (authenticated)
{
FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, LoginControl.RememberMeSet);
}
}
public bool IsAlphaNumeric(string text)
{
return Regex.IsMatch(text, "^[a-zA-Z0-9]+$");
}
private bool ValidateCredentials(string userName, string password)
{
bool returnValue = false;
if (this.IsAlphaNumeric(userName) && userName.Length <= 50 && password.Length <= 50)
{
SqlConnection conn = null;
try
{
string sql = "select count(*) from dbo.Users where UserName = @username and password = @password";
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MembershipSiteConStr"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter user = new SqlParameter();
user.ParameterName = "@username";
user.Value = userName.Trim();
cmd.Parameters.Add(user);
SqlParameter pass = new SqlParameter();
pass.ParameterName = "@password";
//pass.Value = Hasher.HashString(password.Trim());
pass.Value = password.Trim();
cmd.Parameters.Add(pass);
conn.Open();
int count = (int)cmd.ExecuteScalar();
if (count > 0) returnValue = true;
}
下面是一些我的web.config位於根目錄的;
<connectionStrings>
<add name="MembershipSiteConStr" connectionString="Data Source=MYIPADDRESS;Initial Catalog=MembershipSite;User ID=123;Password=123" providerName="System.Data.SqlClient" />
<authentication mode="Forms">
<forms defaultUrl="~/members/member.aspx" loginUrl="~/login.aspx" slidingExpiration="true" timeout="20"></forms>
</authentication>
這web.config文件位於我的〜成員/文件夾;
<authorization>
<deny users="?"/>
</authorization>
這是後端我Memebers.aspx
Just some button clicks nothing else.
是否有人在那裏? :) – Losec