首页 > 其他分享 >生成 Token

生成 Token

时间:2022-10-21 11:14:52浏览次数:65  
标签:userManager System 生成 Token new var using Microsoft

using System.Threading.Tasks;

namespace Microsoft.eShopWeb.ApplicationCore.Interfaces;

public interface ITokenClaimsService
{
    Task<string> GetTokenAsync(string userName);
}

  

using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.eShopWeb.ApplicationCore.Constants;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.eShopWeb.Infrastructure.Identity;

public class IdentityTokenClaimService : ITokenClaimsService
{
    private readonly UserManager<ApplicationUser> _userManager;

    public IdentityTokenClaimService(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public async Task<string> GetTokenAsync(string userName)
    {
        var tokenHandler = new JwtSecurityTokenHandler();
        var key = Encoding.ASCII.GetBytes(AuthorizationConstants.JWT_SECRET_KEY);
        var user = await _userManager.FindByNameAsync(userName);
        var roles = await _userManager.GetRolesAsync(user);
        var claims = new List<Claim> { new Claim(ClaimTypes.Name, userName) };

        foreach (var role in roles)
        {
            claims.Add(new Claim(ClaimTypes.Role, role));
        }

        var tokenDescriptor = new SecurityTokenDescriptor
        {
            Subject = new ClaimsIdentity(claims.ToArray()),
            Expires = DateTime.UtcNow.AddDays(7),
            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
        };
        var token = tokenHandler.CreateToken(tokenDescriptor);
        return tokenHandler.WriteToken(token);
    }
}

参考:eShopWeb

标签:userManager,System,生成,Token,new,var,using,Microsoft
From: https://www.cnblogs.com/friend/p/16812772.html

相关文章

  • AI画画到什么水平了?1行代码生成素描画,又一批人要失业啦!
    大家好,这里是程序员晚枫,GitHub......
  • selenium---生成BeautifulReport报告
    现在遇到的难点不在于看不懂,而是看懂了流程思路把握不了,只能不断的重复刷经验以期待能够完全掌握  https://www.cnblogs.com/qican/p/15273376.html 转原作者seleniu......
  • STP生成树
      用802.1T标准根桥:根桥是整体stp网络的中心根桥优先级选举默认为32768和mac地址组成查看根桥命令displaystp查看mac地址displaybridge根端口:描述接收......
  • Python生成假数据
    1.安装依赖pipinstallFaker-ihttps://pypi.tuna.tsinghua.edu.cn/simple2.使用模块生成fromfakerimportFakerdefproduce_data():fake=Faker(locale=......
  • 谷歌AudioLM :通过歌曲片段生成后续的音乐
    AudioLM是Google的新模型,能够生成与提示风格相同的音乐。该模型还能够生成复杂的声音,例如钢琴音乐或人的对话。结果是它似乎与原版没有区别,这是十分让人惊讶的。为什......
  • java生成指定范围的随机整数
    intmax=5;intmin=2;形成过程0:intsaleNumber=(int)(Math.random()*(max));//0~4 , Math.random()返回[0,max)形成过程1:intsaleNumber=(int)(Math.random......
  • springboot +redis+token
    1、pom.xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId......
  • python DeepRacer生成最优路径和速度
    DeepRacer生成最优路径和速度获取赛道数据从github下载:https://github.com/aws-deepracer-community/deepracer-race-dataraw_data里边就是赛道数据,reinvent2018:是rein......
  • 手动生成dump文件的方法分享
    转自:http://www.java265.com/JavaJingYan/202206/16545842593674.htmldump文件的功能:   1.在特定时刻,将整个储存装置或储存装置之某部分的内容记录在另一储存装......
  • ES生成器函数使用实例
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content......