The simple use of ASP. NET MVC session and Cookies

Source: Internet
Author: User

Objective: To save and display the login information with session and cookies.

Cookies enable:

Controller:

// Save the login username in the cookie New HttpCookie ("cookusername", Username.tostring ());    RESPONSE.COOKIES.ADD (cook);
View Code

View:

<a href="#" class="Dropdown-toggle"data-toggle="dropdown"role="Button"Aria-expanded="false"> <iclass="fa Fa-user-o FA-FW"></i>@if (request.cookies["Cookusername"]!=NULL) {@Request. cookies["Cookusername"].                            Value; }                                                                          <spanclass="Caret"></span> </a>
View Code

Session implementation:

Controller

// Login successfully put username in session session["username"] = username.tostring ();
View Code

View:

<a href="#" class="Dropdown-toggle"data-toggle="dropdown"role="Button"Aria-expanded="false"> <iclass="fa Fa-user-o FA-FW"></i>@if (session["username"]!=NULL) {@Session ["username"].                             ToString (); }                                                                           <spanclass="Caret"></span> </a>
View Code

Setting the session expiration time in Web. config

<system.web><sessionstate mode="InProc " timeout=" ></sessionState>  <!--session Expiration time setting--></system.web>
View Code

The simple use of ASP. NET MVC session and Cookies

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.