public DbFactory(IServiceProvider serviceProvider) { connectionConfigs = new ConcurrentDictionary <string, ConnectionConfig>(StringComparer.OrdinalIgnoreCase); options = serviceProvider.GetTomNetOptions(); logger = serviceProvider.GetLogger <DbFactory>(); GetDbConfigs(); }
/// <summary> /// 生成JwtToken /// </summary> public static string CreateToken(Claim[] claims, TomNetOptions options) { JwtOptions jwtOptions = options.Jwt; string secret = jwtOptions.Secret; if (secret == null) { throw new TomNetException("创建JwtToken时Secret为空"); } SecurityKey key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secret)); SigningCredentials credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature); DateTime now = DateTime.Now; double days = Math.Abs(jwtOptions.ExpireDays) > 0 ? Math.Abs(jwtOptions.ExpireDays) : 7; DateTime expires = now.AddDays(days); SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Audience = jwtOptions.Audience, Issuer = jwtOptions.Issuer, SigningCredentials = credentials, NotBefore = now, IssuedAt = now, Expires = expires }; JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); SecurityToken token = tokenHandler.CreateToken(descriptor); return(tokenHandler.WriteToken(token)); }
/// <summary> /// 发送Email /// </summary> /// <param name="email">接收人Email</param> /// <param name="subject">Email标题</param> /// <param name="body">Email内容</param> /// <returns></returns> public Task SendEmailAsync(string email, string subject, string body) { TomNetOptions options = _provider.GetTomNetOptions(); MailSenderOptions mailSender = options.MailSender; if (mailSender == null || mailSender.Host == null || mailSender.Host.Contains("请替换")) { throw new TomNetException("邮件发送选项不存在,请在appsetting.json配置TomNet.MailSender节点"); } string host = mailSender.Host, displayName = mailSender.DisplayName, userName = mailSender.UserName, password = mailSender.Password; SmtpClient client = new SmtpClient(host) { UseDefaultCredentials = false, Credentials = new NetworkCredential(userName, password) }; string fromEmail = userName.Contains("@") ? userName : "******".FormatWith(userName, client.Host.Replace("smtp.", "")); MailMessage mail = new MailMessage { From = new MailAddress(fromEmail, displayName), Subject = subject, Body = body, IsBodyHtml = true }; mail.To.Add(email); return(client.SendMailAsync(mail)); }
public AjaxResult Jwtoken(LoginDto dto) { Check.NotNull(dto, nameof(dto)); if (!ModelState.IsValid) { return(new AjaxResult("提交信息验证失败", AjaxResultType.Error)); } var user = _userLoginContract.GetFirst(m => m.UserName == dto.UserName && m.Password == dto.Password); if (user == null) { return(new AjaxResult("账户或密码错误", AjaxResultType.Error)); } //生成Token,这里只包含最基本信息,其他信息从在线用户缓存中获取 Claim[] claims = { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.UserName) }; TomNetOptions options = HttpContext.RequestServices.GetTomNetOptions(); string token = JwtHelper.CreateToken(claims, options); return(new AjaxResult("登录成功", AjaxResultType.Success, token)); }