Exemplo n.º 1
0
        public async Task <object> MiniAuth(WeChatMiniProgramAuthenticateModel loginModel)
        {
            var appid = await _setting.GetOrNullAsync(MallManagementSetting.MiniAppId);

            var appSec = await _setting.GetOrNullAsync(MallManagementSetting.MiniAppSecret);

            return(await _weixinAppService.MiniAuth(loginModel, appid, appSec));
        }
Exemplo n.º 2
0
        public async Task <object> MiniAuth(WeChatMiniProgramAuthenticateModel loginModel)
        {
            var appName = _httpContextAccessor?.HttpContext.Request.Headers["AppName"].FirstOrDefault();
            var app     = await _appProvider.GetOrNullAsync(appName);

            var appid  = app["appid"] ?? throw new AbpException($"App:{appName} appid未设置");
            var appSec = app["appsec"] ?? throw new AbpException($"App:{appName} appsec未设置");

            return(await _weixinAppService.MiniAuth(loginModel, appid, appSec));
        }
Exemplo n.º 3
0
 public async Task <object> Code2Session(WeChatMiniProgramAuthenticateModel loginModel)
 {
     return(await Task.FromResult <object>(null));
 }
Exemplo n.º 4
0
        public async Task <object> GetPhone(WeChatMiniProgramAuthenticateModel data)
        {
            var json = Encryption.AES_decrypt(data.encryptedData, data.session_key, data.iv);

            return(await Task.FromResult(json));
        }
Exemplo n.º 5
0
        public async Task <object> MiniAuth(WeChatMiniProgramAuthenticateModel loginModel, string appName)
        {
            var app = await _appProvider.GetOrNullAsync(appName);

            var appid  = app["appid"] ?? throw new AbpException($"App:{appName} appid未设置");
            var appSec = app["appsec"] ?? throw new AbpException($"App:{appName} appsec未设置");

            var session = await _weixinManager.Mini_Code2Session(loginModel.code, appid, appSec);

            // 解密用户信息
            var miniUserInfo =
                await _weixinManager.Mini_GetUserInfo(appid, loginModel.encryptedData, session.session_key,
                                                      loginModel.iv);

            miniUserInfo.AppName = appName;

            // 更新数据库
            await _capBus.PublishAsync("weixin.services.mini.getuserinfo", miniUserInfo);

            var token = "";

            var user = await _identityUserStore.FindByLoginAsync($"unionid", miniUserInfo.unionid);

            if (user == null)
            {
                var userId = _guidGenerator.Create();
                user = new IdentityUser(userId, miniUserInfo.unionid, $"{miniUserInfo.unionid}@somall.top",
                                        _currentTenant.Id)
                {
                    Name = miniUserInfo.nickName
                };

                using (var uow = _unitOfWorkManager.Begin(requiresNew: true))
                {
                    var passHash = _passwordHasher.HashPassword(user, "1q2w3E*");
                    await _identityUserStore.CreateAsync(user);

                    await _identityUserStore.SetPasswordHashAsync(user, passHash);

                    await _identityUserStore.AddLoginAsync(user,
                                                           new UserLoginInfo($"unionid", miniUserInfo.unionid, "unionid"));

                    await _identityUserStore.AddLoginAsync(user,
                                                           new UserLoginInfo($"{appid}_openid", miniUserInfo.openid, "openid"));

                    await _unitOfWorkManager.Current.SaveChangesAsync();

                    await uow.CompleteAsync();
                }
            }

            var serverClient = _httpClientFactory.CreateClient();

            var disco = await serverClient.GetDiscoveryDocumentAsync(_configuration["AuthServer:Authority"]);

            var result = await serverClient.RequestTokenAsync(
                new TokenRequest
            {
                Address   = disco.TokenEndpoint,
                GrantType = "UserWithTenant",

                ClientId     = _configuration["AuthServer:ClientId"],
                ClientSecret = _configuration["AuthServer:ClientSecret"],
                Parameters   =
                {
                    { "user_id",  $"{user.Id}"       },
                    { "tenantid", $"{user.TenantId}" },
                    {
                        "scope", "SoMall"
                    }
                }
            });

            token = result.AccessToken;

            return(await Task.FromResult(new
            {
                AccessToken = token,
                ExternalUser = miniUserInfo,
                SessionKey = session.session_key
            }));
        }
Exemplo n.º 6
0
        public async Task <object> MiniAuth(WeChatMiniProgramAuthenticateModel loginModel)
        {
            var appId = await _setting.GetOrNullAsync(WeixinManagementSetting.MiniAppId);

            var appSec = await _setting.GetOrNullAsync(WeixinManagementSetting.MiniAppSecret);

            var session = await _weixinManager.Mini_Code2Session(loginModel.code, appId, appSec);

            // 解密用户信息
            var miniUserInfo =
                await _weixinManager.Mini_GetUserInfo(appId, loginModel.encryptedData, session.session_key, loginModel.iv);

            // 更新数据库
            await _capBus.PublishAsync("weixin.services.mini.getuserinfo", miniUserInfo);

            var token = "";

            var user = await _identityUserStore.FindByLoginAsync($"{appId}_unionid", miniUserInfo.unionid);

            if (user == null)
            {
                var userId = Guid.NewGuid();
                user = new IdentityUser(userId, miniUserInfo.unionid, $"{miniUserInfo.unionid}@somall.top", _currentTenant.Id);

                using (var uow = _unitOfWorkManager.Begin())
                {
                    var passHash = _passwordHasher.HashPassword(user, "1q2w3E*");
                    await _identityUserStore.CreateAsync(user);

                    await _identityUserStore.SetPasswordHashAsync(user, passHash);

                    await _identityUserStore.AddLoginAsync(user, new UserLoginInfo($"{appId}_unionid", miniUserInfo.unionid, "unionid"));

                    await _identityUserStore.AddLoginAsync(user, new UserLoginInfo($"{appId}_openid", miniUserInfo.openid, "openid"));

                    await _unitOfWorkManager.Current.SaveChangesAsync();

                    await uow.CompleteAsync();

                    return(await Task.FromResult(new
                    {
                        AccessToken = "retry",
                        ExternalUser = miniUserInfo,
                        SessionKey = session.session_key
                    }));
                }
            }

            var serverClient = _httpClientFactory.CreateClient();
            var disco        = await serverClient.GetDiscoveryDocumentAsync("https://localhost:44380");

            var result = await serverClient.RequestTokenAsync(
                new TokenRequest
            {
                Address   = disco.TokenEndpoint,
                GrantType = "password",

                ClientId     = "SoMall_App",
                ClientSecret = "1q2w3e*",
                Parameters   =
                {
                    { "UserName", user.UserName },
                    { "Password", "1q2w3E*"     },
                    { "scope",    "SoMall"      }
                }
            });

            token = result.AccessToken;

            return(await Task.FromResult(new
            {
                AccessToken = token,
                ExternalUser = miniUserInfo,
                SessionKey = session.session_key
            }));
        }
Exemplo n.º 7
0
        public async Task <object> MiniAuth(WeChatMiniProgramAuthenticateModel loginModel)
        {
            var appName = _httpContextAccessor.GetAppName();

            return(await _weixinAppService.MiniAuth(loginModel, appName));
        }