How to implement forms authentication for a directory

Source: Internet
Author: User
Tags config
Some time ago in the study of background login verification problem, I heard that forms of authentication to compare the convenience, so the study, the results found on the internet is not complete (or not suitable for beginners), a lot of problems, now I put my code out, I hope to give you some help.
My background files are all placed in the admin directory, the background of the login is admin_login.aspx, now to achieve the function is:
You cannot directly access files other than admin_login.aspx (that is, login) files in the Admin directory, but the foreground files, such as Default.aspx, are not affected by forms validation, that is, even if you are not logged on, When you visit default.aspx, you cannot go to admin_login.aspx.

Let's say the implementation process:
In the Web.config file, set to forms authentication:
<authentication mode= "Forms" >
<forms name= ". Loginuser "loginurl=" admin/admin_login.aspx "protection=" All "timeout="/>
</authentication>
<authorization>
<allow users= "*"/>
</authorization>

And then after </system.web> to add:

<location path= "Admin" >
<system.web>
<authorization>
<deny users= "?"/>
<allow users= "*"/>
</authorization>
</system.web>
</location>

Otherwise, no matter what file to access, will go to the login, add the above code, only access to the file under the Admin directory may go to admin_login.aspx, the above is the Web.config file settings, followed by the background login button code:

Check the login username and password is correct, I used to check from the database method, you can use other methods, here is not said.
if (Opendata.checkuser (strSQL, "Toempire_userinfo"))
{
Save logged in User name
session["Loginuser"] = Strloginuser;

This is the key, Strloginuser is the user name in the login box. False is not a permanent record.
FormsAuthentication.RedirectFromLoginPage (Strloginuser,false);

Response.Redirect ("admin_index.aspx");
}

The above is the implementation of forms authentication process, I hope to be useful to you. If you have any questions, please leave a message here and I will reply if I see it.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.