3

I'm trying to login by storing username and password in web.config file as below

 <authentication mode="Forms">
  <forms loginUrl="Admin Login.aspx" defaultUrl="~/Home.aspx">
    <credentials>
      <user name="Admin"  password="123"  />

    </credentials>
  </forms>
</authentication>

And I wrote this code on submit button

 if (FormsAuthentication.Authenticate(TxtUsername.Text, TxtPassword.Text))
    {

        Response.Redirect(FormsAuthentication.GetRedirectUrl(TxtUsername.Text, false));
    }

but it's not get redirected.. Any suggestion..?

2 Answers 2

2

You need to call FormsAuthentication.SetAuthCookie() before redirecting.

if (FormsAuthentication.Authenticate(TxtUsername.Text, TxtPassword.Text))
{
    FormsAuthentication.SetAuthCookie(TxtUsername.Text, false);
    Response.Redirect(FormsAuthentication.GetRedirectUrl(TxtUsername.Text, false));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Have you setup an <authorization> section in your web.config? You need to tell ASP.Net to deny users to a certain page, which will cause a redirect to your login form. Does this make sense?
@dana: if there is no authorization forms authentication and redirect should work normally..
2

Actual culprit is your credentials tag..
change it to following.. it should work for you..

..
<credentials passwordFormat="Clear">
     ...

and also use

FormsAuthentication.RedirectFromLoginPage(TxtUsername.Text, false);

after authentication, which will set authCookie and redirect to either requested page or to your default page..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.