标签:
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript"> var header = { "alg": "HS256", "typ": "JWT" }; var payload = { "sub": "1234567890", "name": "John Doe", "admin": true }; </script> </head> <body> <form id="form1" runat="server"> <div> <a href="Default.aspx?jwt=111">登陆</a> </div> <div id="div_2" style="width:500px;" runat="server"></div> <h1> <asp:Button ID="btn_1" runat="server" Text="查看" OnClick="btn_1_Click" /> </h1> <div id ="div_1" runat="server" style="border:solid 1px;"></div> </form> </body> </html>
.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IdentityModel.Tokens; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(Request.QueryString["jwt"] != null && Request.QueryString["jwt"] != "") { div_2.InnerHtml = Write(); } //issuer 发行者 //audience 观众 // } public string Write() { //var issuer = "localhost"; //发行者 //var audience = "all"; //观众 var xx = new List<System.Security.Claims.Claim> { new System.Security.Claims.Claim("name","huang"), new System.Security.Claims.Claim("password","123456") }; //1234567890123456 原值 //MTIzNDU2Nzg5MDEyMzQ1Ng== base64加密后 var key = Convert.FromBase64String("MTIzNDU2Nzg5MDEyMzQ1Ng=="); var now = DateTime.Now; //现在时间 var expires = now.AddMinutes(10); //过期时间 var signingCredentials = new SigningCredentials(new InMemorySymmetricSecurityKey(key), "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "http://www.w3.org/2001/04/xmlenc#sha256"); var token = new JwtSecurityToken(null, null,xx,now, expires,signingCredentials); return new JwtSecurityTokenHandler().WriteToken(token); } public void Read(string token) { string zz = ""; JwtSecurityToken xx = new JwtSecurityToken(token); zz += "{ \"typ\": "; zz += xx.Header.Typ + ", \"alg\" " + xx.Header.Alg; zz += " }\r\n"; zz += "{"; zz += "{"; zz += "{"; zz += "{"; zz += "{"; zz += "{"; zz += "{"; div_1.InnerHtml = zz; } protected void btn_1_Click(object sender, EventArgs e) { Read(div_2.InnerHtml); } }
引用dll:System.IdentityModel.Tokens.Jwt.dll
相关网站:
http://jwt.io/
http://odetocode.com/blogs/scott/archive/2015/01/15/using-json-web-tokens-with-katana-and-webapi.aspx
标签:
原文地址:http://www.cnblogs.com/revoid/p/5613536.html