asp.net form validation

Source: Internet
Author: User

Reading the most commonly used to judge landing is to log in with cookies or session storage, and then each page to determine whether the landing.

It's a hassle to do every page.

Graduated six months, this six months learned to use classes, rewrite the event onload (), so that each page inherits this class! This class can determine whether to log in, Judge permissions and so on some operations

However, if only login verification does not have permission to other operations! This is more convenient to use form validation than inheriting classes, and this is the only thing that comes to mind today! As a result of reading attention to play, the results of many did not pay attention to listen. Today I've learned the next form validation

First configure Web.config

Configuring within Systen.web


<authentication mode= "Forms" >
<forms loginurl= "admin/login.aspx" defaulturl= "admin/default.aspx" timeout= "1" ></forms>
</authentication>
<!--
The Name property specifies the names of the cookies required for authentication, and the default value is ". Aspxauth, if you have multiple WEB applications hanging up and down on a server, you must specify the name again because each application requires a unique cookie.

The Loginurl property specifies the page to use to provide the user name and password, and the default value is "Login.aspx". This page can be placed in the same directory as the page that requires authentication to be accessed (oh, I thought this page would be in a separate, publicly accessible directory).

The Defaulturl property specifies the page to jump to after the login, the default value is "default.aspx", and of course you can jump to the previous page before the user login, and this is the default implementation of. NET.
Timeout cookie Expiration Time
-->

<!--system.web Peer-->
<location path= "Admin" >
<system.web>
<authorization>
<deny users= "?" /><--denies anonymous users-->
</authorization>
</system.web>
</location>

I do the backstage in a separate folder! So as long as you can set permissions on this folder, if you do not log in the folder to browse any file will jump to the landing page, landing successfully jump to ReturnUrl, which is the page you visited before. If ReturnUrl is empty, jump to Defaulturl, which is the default URL you configure

Next is the code

Code


if (TextBox1.Text = = "Adminss" && TextBox2.Text = "admin")
      & nbsp {
           formsauthentication.redirectfromloginpage ( TextBox1.Text, checkbox1.checked);
           //checkbox1.checked If True, the cookie will be persisted. Until the cookie expires
           //Can be followed by Response.Redirect (" Xx.aspx "), will jump to the page you specify, but can not be no verification on the jump!
           //failure to jump directly without the above verification ticket because no authentication ticket is established
           //response.redirect (context.request[" ReturnUrl "]);
           //response.redirect ("xx.aspx");
       }
        Else
            Response.Write ("Username or password is wrong!") ");

Some pages need to be judged by whether the user is logged into different layouts

if (User.Identity.IsAuthenticated)//This is used to determine whether the user login
{
Has landed;
}
Else
{
Not logged in
}

Sometimes the user's user name is displayed on the page

User.Identity.Name // get the name of the landing, is actually before FormsAuthentication.RedirectFromLoginPage (TextBox1.Text, checkbox1.checked); TextBox1.Text;

I love plastic body Net: http://www.52sushen.com

Okay, then it's the exit code.

FormsAuthentication.SignOut ();

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.