예제 #1
0
        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private async Task <List <Claim> > ValidateUserAsync(string loginName, string password)
        {
            //TODO 这里可以通过用户名和密码到数据库中去验证是否存在,
            // 以及角色相关信息,我这里还是使用内存中已经存在的用户和密码
            var user = ID4Config.GetUsers();

            if (user == null)
            {
                throw new Exception("登录失败,用户名和密码不正确");
            }
            return(new List <Claim>()
            {
                new Claim(ClaimTypes.Name, $"{loginName}"),
            });
        }
예제 #2
0
        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private async Task <List <Claim> > ValidateUserAsync(string openId, string unionId)
        {
            //TODO 这里可以通过openId 和unionId 来查询用户信息(数据库查询),
            //我这里为了方便测试还是直接写测试的openId 相关信息用户
            var user = ID4Config.GetWeiXinOpenIdTestUsers();

            if (user == null)
            {
                //注册用户
            }

            return(new List <Claim>()
            {
                new Claim(ClaimTypes.Name, $"{openId}"),
            });
        }
예제 #3
0
        public async Task <Client> FindClientByIdAsync(string clientId)
        {
            //  return GetClient(clientId);
            #region 用户名密码
            var memoryClients = ID4Config.GetClients();
            if (memoryClients.Any(oo => oo.ClientId == clientId))
            {
                return(memoryClients.FirstOrDefault(oo => oo.ClientId == clientId));
            }
            #endregion

            #region 通过数据库查询Client 信息
            return(GetClient(clientId));

            #endregion
        }
예제 #4
0
        public async Task <List <Claim> > ValidateUserByRoleAsync(string loginName, string password)
        {
            var user = ID4Config.GetUserByUserName(loginName);

            if (user == null)
            {
                throw new Exception("登录失败,用户名和密码不正确");
            }
            //实际生产环境需要通过读取数据库的信息并且来声明
            return(new List <Claim>()
            {
                new Claim(ClaimTypes.Name, $"{user.UserName}"),
                new Claim(EnumUserClaim.DisplayName.ToString(), user.DisplayName),
                new Claim(EnumUserClaim.UserId.ToString(), user.UserId.ToString()),
                new Claim(EnumUserClaim.ProviderId.ToString(), user.ProviderId.ToString()),
                new Claim(JwtClaimTypes.Role.ToString(), user.Role.ToString())
            });
        }
예제 #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var migrationsAssembly = "CommonScheme.IdentityAPI";
            //string connectionString = @"Data Source=ZYXTJSTD-WUTJ\MSSQLSERVER12;Initial Catalog=IdentityServerDB;Persist Security Info=True;User ID=sa;Password=wutengjian123";
            var connectionString = Configuration.GetConnectionString("UserCenterConnection");

            services.AddMvc();
            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            // .AddConfigurationStore(options =>
            // {
            //     options.ConfigureDbContext = builder => builder.UseMySql(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
            //     //options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
            // })
            //.AddOperationalStore(options =>
            //{
            //    options.ConfigureDbContext = builder => builder.UseMySql(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
            //    //options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
            //    options.EnableTokenCleanup = true;
            //    options.TokenCleanupInterval = 300;
            //})
            #region 内存方式
            .AddInMemoryIdentityResources(ID4Config.GetIdentityResources())
            .AddInMemoryApiResources(ID4Config.GetApis())
            .AddInMemoryClients(ID4Config.GetClients())
            .AddTestUsers(ID4Config.GetUsers())
            #endregion

            #region 数据库存储方式
            //.AddClientStore<ClientStore>()
            //.AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()//账户密码方式验证
            //.AddExtensionGrantValidator<WeiXinOpenGrantValidator>()//添加微信端自定义方式的验证
            ;
            #endregion
            // services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }