Пример #1
0
        public Customer GetUser(WeChatUserInfo userInfo)
        {
            var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
            {
                ExternalIdentifier        = userInfo.openid,
                ExternalDisplayIdentifier = userInfo.unionid,
            };

            return(_openAuthenticationService.GetUser(parameters));
        }
Пример #2
0
        private AuthorizeState VerifyAuthentication(string returnUrl)
        {
            var            returnString = _httpContext.Request.QueryString;
            var            sortedStr    = returnString.AllKeys;
            WeChatUserInfo userInfo     = null;
            SortedDictionary <string, string> sortedDic = new SortedDictionary <string, string>();

            for (int i = 0; i < sortedStr.Length; i++)
            {
                sortedDic.Add(sortedStr[i], returnString[sortedStr[i]]);
            }
            if (sortedDic.Keys.Contains("code"))
            {
                var code = sortedDic["code"];
                userInfo = GetUserInfo(code);
            }
            if (userInfo == null)
            {
                var currentCustomerId = _workContext.CurrentCustomer.Id;
                if (currentCustomerId != 0)
                {
                    var key = string.Format(WEIXIN_USER_CUSTOMERID, currentCustomerId);
                    userInfo = _cacheManager.Get <WeChatUserInfo>(key);
                    _cacheManager.Remove(key);
                }
            }
            var email = _httpContext.Request.Form["Email"]; //获取注册邮箱

            if (userInfo != null)
            {
                var parameters = new OAuthAuthenticationParameters(Provider.SystemName)
                {
                    ExternalIdentifier        = userInfo.openid,
                    ExternalDisplayIdentifier = userInfo.unionid
                };
                // if (_externalAuthenticationSettings.AutoRegisterEnabled)
                ParseClaims(userInfo, parameters, email);
                var result = _authorizer.Authorize(parameters);
                return(new AuthorizeState(returnUrl, result));
            }
            else
            {
                var state = new AuthorizeState(returnUrl, OpenAuthenticationStatus.Error);
                var error = "获取用户信息失败";
                state.AddError(error);
                return(state);
            }
        }
Пример #3
0
        private void ParseClaims(WeChatUserInfo userInfo, OAuthAuthenticationParameters parameters
                                 , string email)
        {
            Random random = new Random();
            var    claims = new UserClaims();

            claims.Contact = new ContactClaims()
            {
                Email = email
            };
            claims.Contact.Address = new AddressClaims()
            {
                City    = userInfo.city,
                State   = userInfo.province,
                Country = userInfo.country,
            };
            claims.Name = new NameClaims()
            {
                Nickname = userInfo.nickname,
            };

            parameters.AddClaim(claims);
        }