I want the user to be redirected to the login page if not logged in. The login credentials are "Admin" & "Password" always. When I log in, it redirects me to the protected files and that's exactly what I wanted. HOWEVER, I can also navigate to the Protected files without logging in. What would be the best solution? Is it something to do with my Web.Config? Beneath is my authorization control for my Account folder which has got the login.aspx and I want to protect files inside my folder /Private if the user can't log in.
<location path="Account">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
Looking forward to your help!
Here is my Login.aspx's event handler after Login button click:
protected void LogIn(object sender, EventArgs e)
{
if (FormsAuthentication.Authenticate(UserName.Text, Password.Text))
{
var persistCookie = false;
FormsAuthentication.RedirectFromLoginPage(UserName.Text, persistCookie);
}
if (IsValid)
{
string uname = UserName.Text.ToString().Replace(" ", "").ToString();
string password = Password.Text.ToString().Replace(" ", "").ToString();
if (String.Equals(uname, "Admin") && String.Equals(password, "MG32015!"))
{
Session["user"] = uname;
Response.Redirect("~/Private/ViewEnquiry.aspx");
//IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
And the Logout.aspx.cs has this:
public partial class Account_Login : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session.Clear();
FormsAuthentication.SignOut();
//Response.Redirect("Login.aspx");
}
protected void Login_Click(object sender, EventArgs e)
{
Response.Redirect("~/Account/Login.aspx");
}
}