zoukankan      html  css  js  c++  java
  • JWT使用---来源practical-aspnetcore项目

    1、配置Setup

            public void ConfigureServices(IServiceCollection services)
            {
                services.Configure<JwtIssuerOptions>(options =>
                {
                    options.Issuer = "SimpleServer";
                    options.Audience = "http://localhost";
                    options.SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes("12345678901234567890")), SecurityAlgorithms.HmacSha256);
    
                });
    
                services.AddControllersWithViews();
            }

    2、生成JWT的输出token

                var claims = new[]
                {
                    new Claim(ClaimTypes.Name, "Celon"),
                    new Claim(ClaimTypes.Role, "Admin")
                };
    
                var option = _options.Value;
    
                var token = new JwtSecurityToken
                (
                    issuer: option.Issuer,
                    audience: option.Audience,
                    claims: claims,
                    expires: DateTime.Now.AddMinutes(60),
                    signingCredentials: option.SigningCredentials
                );
    
                var outputToken = new JwtSecurityTokenHandler().WriteToken(token);

    3、解析token

                var jwt = new JwtSecurityToken(token);
  • 相关阅读:
    抚琴弹唱东流水
    借点阳光给你
    日月成双行影单
    一夜飘雪入冬来
    悼念钱学森
    我的青春谁作主
    重游望江楼有感
    雪后暖阳
    满城尽添黄金装
    敢叫岁月不冬天
  • 原文地址:https://www.cnblogs.com/CelonY/p/13717240.html
Copyright © 2011-2022 走看看