Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Web.Security;
Namespace verify permissions. Controllers
{
public class Homecontroller:controller
{
//
GET:/home/
Public ActionResult Index ()
{
The first way to get a cookie
HttpCookie cookie = request.cookies["Ticket"];
Revert to ticket object after decryption
FormsAuthenticationTicket ticket = Formsauthentication.decrypt (cookie. Value);
Response.Write ("User name =" +ticket. Name + "Permission =" +ticket. UserData);
Second use of Microsoft Authentication Mechanism authorization
if (HttpContext.Request.IsAuthenticated)
{
String username= httpcontext.user.identity.name;//Get user name
FormsIdentity formsidentity= HttpContext.User.Identity as formsidentity; Identity information
FormsIdentity. Ticket;
Landed.
}
Else
{
Not logged in
}
return View ();
}
Public ActionResult Login ()
{
return View ();
}
[HttpPost]
Public ActionResult Login (formcollection form)
{
if (form["txtname"] = = "James" && form["txtpwd"] = = "123")
{
#region Save cookies Using Microsoft Notes encryption
Save cookies using Microsoft Notes encryption
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1, "James", DateTime. Now, DateTime. Now. AddMinutes (5), True, "all-in-all");
Convert a ticket object to an encrypted string
String Hashcookie = Formsauthentication.encrypt (ticket);
HttpCookie Cokie = new HttpCookie ("Ticket", Hashcookie);
Cokie. Expires = DateTime.Now.AddMinutes (5);
RESPONSE.COOKIES.ADD (Cokie);
Response.Write ("Landing success!") ");
#endregion
#region Use Microsoft's own authentication mechanism
Using Microsoft's own authentication mechanism
Formsauthentication.setauthcookie ("James", true);
#endregion
Combine manual creation of notes and develop login user rights flag string
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1, "James", DateTime. Now, DateTime. Now. AddMinutes (5), True, "all-in-all");
Convert a ticket object to an encrypted string
String Hashcookie = Formsauthentication.encrypt (ticket);
HttpCookie Cokie = new HttpCookie (Formsauthentication.formscookiename, Hashcookie);//formsauthentication. FormsCookieName the cookie name of the configuration file
Cokie. Expires = DateTime.Now.AddMinutes (5);
RESPONSE.COOKIES.ADD (Cokie);
}
return View ();
}
}
}