MVC Microsoft Note Verification

Source: Internet
Author: User
Tags httpcontext

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 ();
}


}
}

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.