2 Star 0 Fork 0

github__fork/EasyCode

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TokenProviderService.cs 2.71 KB
一键复制 编辑 原始数据 按行查看 历史
luckearth 提交于 2019-08-05 15:28 +08:00 . 挂数据库生成Token
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using EasyCode.Core;
using EasyCode.Core.Data;
using EasyCode.Data;
using EasyCode.Entity;
using EasyCode.IService;
using EasyCode.ViewModel;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace EasyCode.Service
{
public class TokenProviderService : ITokenProviderService
{
private TokenProviderOptions _options;
private IUnitOfWork<EasyCodeContext> _unitOfWork;
public TokenProviderService(TokenProviderOptions options, IUnitOfWork<EasyCodeContext> unitOfWork)
{
_options = options;
_unitOfWork = unitOfWork;
}
public ResponseTokenViewModel GeTokenViewModel(string username,string userid)
{
var refreshtoken = Guid.NewGuid().ToString("N");
var now = DateTime.UtcNow;
var claims = new List<Claim>()
{
new Claim(JwtRegisteredClaimNames.NameId,""),
new Claim(JwtRegisteredClaimNames.Jti, ""),
new Claim(JwtRegisteredClaimNames.Iat, new DateTimeOffset(now).ToUniversalTime().ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64),
new Claim(JwtRegisteredClaimNames.Sub, username),
};
var jwt = new JwtSecurityToken(
issuer: _options.Issuer,
audience: _options.Audience,
claims: claims.ToArray(),
notBefore: now,
expires: now.AddMinutes(_options.Expiration),
signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_options.SecretKey)), SecurityAlgorithms.HmacSha256));
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
_unitOfWork.DbContext.SysUserTokenses.Add(new SysUserTokens(){ ExpiresUtc = DateTime.UtcNow,Token = encodedJwt,UserId = userid, Value = refreshtoken,LoginProvider = "",Name = username});
_unitOfWork.SaveChanges();
ResponseTokenViewModel model = new ResponseTokenViewModel()
{
access_token = encodedJwt,
refresh_token = refreshtoken,
expires_in = (int)TimeSpan.FromSeconds(_options.Expiration*60).TotalSeconds,
userName = username,
firstname = username,
lastname = username,
createtime = DateTime.Now
};
return model;
}
public ResponseTokenViewModel GetRefreshToken(string refreshToken)
{
var model = GeTokenViewModel("","");
return model;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/github__fork/EasyCode.git
git@gitee.com:github__fork/EasyCode.git
github__fork
EasyCode
EasyCode
master

搜索帮助