Exemplo n.º 1
0
        public static bool TryWechatAutoLogin(this IAccessControl ac, bool external, out string openId)
        {
            var app = ac.Context.RequestServices.GetService <IWeChatContainer>().GetApp();

            try
            {
                var code   = App.GetQuery("code", "");
                var result = OAuthApi.GetAccessToken(app.AppId, app.AppSecret, code);
                openId = result.openid;
            }
            catch
            {
                openId = "";
            }

            if (openId.HasValue())
            {
                if (external)
                {
                    return(true);
                }

                var service = ac.Context.RequestServices.GetService <IUserService>();
                var user    = service.QueryByAuth(app.Id, openId);
                if (user != null)
                {
                    ac.Login(user, true).Wait();
                    return(true);
                }
                return(false);
            }

            return(false);
        }
Exemplo n.º 2
0
        public static async Task WechatLogin(this IAccessControl ac, string userName, string password, string openId)
        {
            var app         = ac.Context.RequestServices.GetService <IWeChatContainer>().GetApp();
            var userService = ac.Context.RequestServices.GetService <IUserService>();
            var user        = userService.CheckUser(userName, password);

            var userInfoJson = UserApi.Info(app.AppId, openId);

            if (userInfoJson != null)
            {
                user.NickName = userInfoJson.nickname;
                user.Photo    = userInfoJson.headimgurl;
                userService.Update(user);
            }

            userService.UpdateAuth(user, app.Id, openId, "wechat");
            await ac.Login(user, true);
        }