public override string GetConnectionString() { if (_serviceProvider == null) { string str = AppSettingsManager.Get("OSharp:DbContexts:MySql:ConnectionString"); return(str); } OSharpOptions options = _serviceProvider.GetOSharpOptions(); OSharpDbContextOptions contextOptions = options.GetDbContextOptions(typeof(DefaultDbContext)); if (contextOptions == null) { throw new OsharpException($"上下文“{typeof(DefaultDbContext)}”的配置信息不存在"); } return(contextOptions.ConnectionString); }
public async Task <AjaxResult> Jwtoken(LoginDto dto) { Check.NotNull(dto, nameof(dto)); if (!ModelState.IsValid) { return(new AjaxResult("提交信息验证失败", AjaxResultType.Error)); } dto.Ip = HttpContext.GetClientIp(); dto.UserAgent = Request.Headers["User-Agent"].FirstOrDefault(); OperationResult <User> result = await _identityContract.Login(dto); IUnitOfWork unitOfWork = HttpContext.RequestServices.GetUnitOfWork <User, int>(); unitOfWork.Commit(); if (!result.Successed) { return(result.ToAjaxResult()); } User user = result.Data; //生成Token,这里只包含最基本信息,其他信息从在线用户缓存中获取 Claim[] claims = { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.UserName) }; OSharpOptions options = HttpContext.RequestServices.GetService <IOptions <OSharpOptions> >().Value; string token = JwtHelper.CreateToken(claims, options); //在线用户缓存 IOnlineUserCache onlineUserCache = HttpContext.RequestServices.GetService <IOnlineUserCache>(); if (onlineUserCache != null) { await onlineUserCache.GetOrRefreshAsync(user.UserName); } return(new AjaxResult("登录成功", AjaxResultType.Success, token)); }
/// <summary> /// 应用模块服务 /// </summary> /// <param name="provider">服务提供者</param> public override void UsePack(IServiceProvider provider) { OSharpOptions options = provider.GetOSharpOptions(); OSharpDbContextOptions contextOptions = options.GetDbContextOptions(typeof(TDbContext)); if (contextOptions?.DatabaseType != DatabaseType) { return; } using (IServiceScope scope = provider.CreateScope()) { TDbContext context = CreateDbContext(scope.ServiceProvider); if (context != null && contextOptions.AutoMigrationEnabled) { context.CheckAndMigration(); DbContextModelCache modelCache = scope.ServiceProvider.GetService <DbContextModelCache>(); modelCache?.Set(context.GetType(), context.Model); } } IsEnabled = true; }