示例#1
0
            /// <summary>
            /// 回调方法
            /// </summary>
            /// <param name="authorizeResult">接收授权码、防伪参数</param>
            public void AuthCallback(LoginBase.AuthorizeResult authorizeResult)
            {
                if (string.IsNullOrWhiteSpace(authorizeResult.code))
                {
                    //打开链接没登录授权
                }
                else
                {
                    //唯一标示
                    string OpenId = string.Empty;

                    switch (loginType)
                    {
                    case LoginBase.LoginType.QQ:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        //获取 UserInfo
                        _ = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });

                        //身份唯一标识
                        OpenId = openidEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.WeiBo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        //获取 users/show
                        _ = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });

                        OpenId = tokenEntity.access_token;
                    }
                    break;

                    case LoginBase.LoginType.WeChat:
                    {
                        //获取 access_token
                        var tokenEntity = WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        _ = WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = tokenEntity.openid
                            });

                        //身份唯一标识
                        OpenId = tokenEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.GitHub:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = GitHub.User(new GitHub_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.TaoBao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        OpenId = tokenEntity.open_uid;
                    }
                    break;

                    case LoginBase.LoginType.MicroSoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.DingTalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);

                        OpenId = userEntity?.openid;
                    }
                    break;

                    case LoginBase.LoginType.Gitee:
                    {
                        //获取 access_token
                        var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = Gitee.User(new Gitee_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.id.ToString();
                    }
                    break;
                    }

                    //拿到登录标识
                    if (string.IsNullOrWhiteSpace(OpenId))
                    {
                        //TO DO
                    }
                }
            }
示例#2
0
            /// <summary>
            /// 回调方法
            /// </summary>
            /// <param name="authorizeResult">接收授权码、防伪参数</param>
            public void AuthCallback(LoginBase.AuthorizeResult authorizeResult)
            {
                if (!string.IsNullOrWhiteSpace(authorizeResult.code) || !string.IsNullOrWhiteSpace(authorizeResult.auth_code))
                {
                    //唯一标识
                    string OpenId = string.Empty;

                    switch (loginType)
                    {
                    case LoginBase.LoginType.QQ:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(tokenEntity.access_token);

                        //获取 UserInfo
                        _ = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });

                        //身份唯一标识
                        OpenId = openidEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.WeiBo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(tokenEntity.access_token);

                        //获取 users/show
                        _ = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });

                        OpenId = tokenEntity.access_token;
                    }
                    break;

                    case LoginBase.LoginType.WeChat:
                    {
                        //获取 access_token
                        var tokenEntity = Login.WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        _ = Login.WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = tokenEntity.openid
                            });

                        //身份唯一标识
                        OpenId = tokenEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.GitHub:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = GitHub.User(tokenEntity.access_token);

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.TaoBao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        OpenId = tokenEntity.open_uid;
                    }
                    break;

                    case LoginBase.LoginType.MicroSoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = MicroSoft.User(tokenEntity.access_token);

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.DingTalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);

                        OpenId = userEntity?.openid;
                    }
                    break;

                    case LoginBase.LoginType.Gitee:
                    {
                        //获取 access_token
                        var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = Gitee.User(tokenEntity.access_token);

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.Google:
                    {
                        //获取 access_token
                        var tokenEntity = Google.AccessToken(new Google_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = Google.User(tokenEntity.access_token);

                        OpenId = userEntity.sub;
                    }
                    break;

                    case LoginBase.LoginType.AliPay:
                    {
                        //获取 access_token
                        var tokenEntity = AliPay.AccessToken(new AliPay_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.auth_code
                            });

                        //实际上这一步已经获取到 OpenId,登录验证可以了,获取个人信息还需调用下面的接口
                        //tokenEntity.user_id

                        //获取 user
                        var userEntity = AliPay.User(new AliPay_User_RequestEntity()
                            {
                                auth_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.user_id;
                    }
                    break;

                    case LoginBase.LoginType.StackOverflow:
                    {
                        //获取 access_token
                        var tokenEntity = StackOverflow.AccessToken(new StackOverflow_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = StackOverflow.User(new StackOverflow_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.user_id;
                    }
                    break;
                    }

                    //拿到登录标识
                    if (string.IsNullOrWhiteSpace(OpenId))
                    {
                        //TO DO
                    }
                }
            }
示例#3
0
        /// <summary>
        /// 登录授权回调
        /// </summary>
        /// <param name="authorizeResult">获取授权码以及防伪标识</param>
        /// <returns></returns>
        public IActionResult AuthCallback(LoginBase.AuthorizeResult authorizeResult)
        {
            var vm = new SharedResultVM();

            try
            {
                if (string.IsNullOrWhiteSpace(authorizeResult.code))
                {
                    vm.Set(SharedEnum.RTag.unauthorized);
                }
                else
                {
                    //唯一标示
                    string openId = string.Empty;
                    //注册信息
                    var mo = new Domain.UserInfo()
                    {
                        LoginLimit     = 0,
                        UserSex        = 0,
                        UserCreateTime = DateTime.Now
                    };
                    //头像
                    string avatar = string.Empty;

                    Enum.TryParse(RouteData.Values["id"]?.ToString(), true, out LoginBase.LoginType vtype);

                    switch (vtype)
                    {
                    case LoginBase.LoginType.QQ:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(tokenEntity.access_token);
                        Console.WriteLine(openidEntity.ToJson());

                        //获取 UserInfo
                        var userEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });
                        Console.WriteLine(userEntity.ToJson());

                        //身份唯一标识
                        openId     = openidEntity.openid;
                        mo.OpenId1 = openId;

                        mo.Nickname = userEntity.nickname;
                        mo.UserSex  = userEntity.gender == "男" ? 1 : 2;
                        mo.UserSay  = "";
                        mo.UserUrl  = "";

                        avatar = userEntity.figureurl_2;
                    }
                    break;

                    case LoginBase.LoginType.WeiBo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(tokenEntity.access_token);
                        Console.WriteLine(tokenInfoEntity.ToJson());

                        //获取 users/show
                        var userEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });
                        Console.WriteLine(userEntity.ToJson());

                        openId     = tokenEntity.access_token;
                        mo.OpenId2 = openId;

                        mo.Nickname = userEntity.screen_name;
                        mo.UserSex  = userEntity.gender == "m" ? 1 : userEntity.gender == "f" ? 2 : 0;
                        mo.UserSay  = userEntity.description;
                        mo.UserUrl  = userEntity.domain;

                        avatar = userEntity.avatar_large;
                    }
                    break;

                    case LoginBase.LoginType.WeChat:
                    {
                        //获取 access_token
                        var tokenEntity = Netnr.Login.WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //openId = tokenEntity.openid;

                        //获取 user
                        var userEntity = Netnr.Login.WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = tokenEntity.openid
                            });
                        Console.WriteLine(userEntity.ToJson());

                        avatar = userEntity.headimgurl;
                    }
                    break;

                    case LoginBase.LoginType.GitHub:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = GitHub.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        openId     = userEntity.id.ToString();
                        mo.OpenId3 = openId;

                        mo.Nickname = userEntity.name;
                        mo.UserSay  = userEntity.bio;
                        mo.UserUrl  = userEntity.blog;
                        mo.UserMail = userEntity.email;

                        avatar = userEntity.avatar_url;
                    }
                    break;

                    case LoginBase.LoginType.Gitee:
                    {
                        //获取 access_token
                        var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = Gitee.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        //openId = userEntity.id.ToString();

                        mo.Nickname = userEntity.name;
                        mo.UserSay  = userEntity.bio;
                        mo.UserUrl  = userEntity.blog;

                        avatar = userEntity.avatar_url;
                    }
                    break;

                    case LoginBase.LoginType.TaoBao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        openId     = tokenEntity.open_uid;
                        mo.OpenId4 = openId;

                        mo.Nickname = "淘宝用户";
                    }
                    break;

                    case LoginBase.LoginType.MicroSoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = MicroSoft.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        openId     = userEntity.id;
                        mo.OpenId5 = openId;

                        mo.Nickname = userEntity.last_name + userEntity.first_name;
                        mo.UserMail = userEntity.emails?["account"].ToStringOrEmpty();
                    }
                    break;

                    case LoginBase.LoginType.DingTalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);
                        Console.WriteLine(userEntity.ToJson());

                        openId     = userEntity.openid;
                        mo.OpenId6 = openId;

                        mo.Nickname = userEntity.nick;
                    }
                    break;

                    case LoginBase.LoginType.Google:
                    {
                        //获取 access_token
                        var tokenEntity = Google.AccessToken(new Google_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = Google.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        //openId = userEntity.sub;

                        avatar = userEntity.picture;
                    }
                    break;

                    case LoginBase.LoginType.AliPay:
                    {
                        //获取 access_token
                        var tokenEntity = AliPay.AccessToken(new AliPay_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //openId = tokenEntity.user_id;

                        //获取 user
                        var userEntity = AliPay.User(new AliPay_User_RequestEntity()
                            {
                                auth_token = tokenEntity.access_token
                            });
                        Console.WriteLine(userEntity.ToJson());

                        avatar = userEntity.avatar;
                    }
                    break;

                    case LoginBase.LoginType.StackOverflow:
                    {
                        //获取 access_token
                        var tokenEntity = StackOverflow.AccessToken(new StackOverflow_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = StackOverflow.User(new StackOverflow_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });
                        Console.WriteLine(userEntity.ToJson());

                        //openId= userEntity.user_id;

                        avatar = userEntity.profile_image;
                    }
                    break;
                    }

                    mo.UserCreateTime = DateTime.Now;
                    mo.UserName       = openId;
                    mo.UserPwd        = CalcTo.MD5(openId);
                    if (!string.IsNullOrWhiteSpace(avatar))
                    {
                        mo.UserPhoto = UniqueTo.LongId().ToString() + ".jpg";
                    }
                    Console.WriteLine(mo.ToJson());

                    if (string.IsNullOrWhiteSpace(openId))
                    {
                        vm.Set(SharedEnum.RTag.unauthorized);
                        vm.Msg = "身份验证失败";
                    }
                    else
                    {
                        //判断是绑定操作
                        bool isbind = User.Identity.IsAuthenticated && authorizeResult.state.StartsWith("bind");
                        if (isbind)
                        {
                            int uid = Apps.LoginService.Get(HttpContext).UserId;

                            //检测是否绑定其它账号
                            var queryIsBind = db.UserInfo.Where(x => x.UserId != uid);
                            switch (vtype)
                            {
                            case LoginBase.LoginType.QQ:
                                queryIsBind = queryIsBind.Where(x => x.OpenId1 == openId);
                                break;

                            case LoginBase.LoginType.WeiBo:
                                queryIsBind = queryIsBind.Where(x => x.OpenId2 == openId);
                                break;

                            case LoginBase.LoginType.GitHub:
                                queryIsBind = queryIsBind.Where(x => x.OpenId3 == openId);
                                break;

                            case LoginBase.LoginType.TaoBao:
                                queryIsBind = queryIsBind.Where(x => x.OpenId4 == openId);
                                break;

                            case LoginBase.LoginType.MicroSoft:
                                queryIsBind = queryIsBind.Where(x => x.OpenId5 == openId);
                                break;

                            case LoginBase.LoginType.DingTalk:
                                queryIsBind = queryIsBind.Where(x => x.OpenId6 == openId);
                                break;
                            }
                            if (queryIsBind.Any())
                            {
                                return(Content("已绑定其它账号,不能重复绑定"));
                            }

                            var userInfo = db.UserInfo.Find(uid);

                            switch (vtype)
                            {
                            case LoginBase.LoginType.QQ:
                                userInfo.OpenId1 = openId;
                                break;

                            case LoginBase.LoginType.WeiBo:
                                userInfo.OpenId2 = openId;
                                break;

                            case LoginBase.LoginType.GitHub:
                                userInfo.OpenId3 = openId;
                                break;

                            case LoginBase.LoginType.TaoBao:
                                userInfo.OpenId4 = openId;
                                break;

                            case LoginBase.LoginType.MicroSoft:
                                userInfo.OpenId5 = openId;
                                break;

                            case LoginBase.LoginType.DingTalk:
                                userInfo.OpenId6 = openId;
                                break;
                            }
                            db.UserInfo.Update(userInfo);
                            db.SaveChanges();

                            return(Redirect("/user/setting"));
                        }
                        else
                        {
                            Domain.UserInfo vmo = null;
                            switch (vtype)
                            {
                            case LoginBase.LoginType.QQ:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId1 == openId);
                                break;

                            case LoginBase.LoginType.WeiBo:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId2 == openId);
                                break;

                            case LoginBase.LoginType.GitHub:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId3 == openId);
                                break;

                            case LoginBase.LoginType.TaoBao:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId4 == openId);
                                break;

                            case LoginBase.LoginType.MicroSoft:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId5 == openId);
                                break;

                            case LoginBase.LoginType.DingTalk:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId6 == openId);
                                break;
                            }
                            //未注册
                            if (vmo == null)
                            {
                                var ruvm = RegisterUser(mo);
                                if (ruvm.Code == 200)
                                {
                                    vm = ValidateLogin(vtype, mo);
                                    //拉取头像
                                    if (vm.Code == 200 && !string.IsNullOrWhiteSpace(avatar))
                                    {
                                        try
                                        {
                                            //物理根路径
                                            var prp   = GlobalTo.GetValue("StaticResource:PhysicalRootPath").Replace("~", GlobalTo.ContentRootPath);
                                            var ppath = PathTo.Combine(prp, GlobalTo.GetValue("StaticResource:AvatarPath"));

                                            if (!Directory.Exists(ppath))
                                            {
                                                Directory.CreateDirectory(ppath);
                                            }

                                            HttpTo.DownloadSave(HttpTo.HWRequest(avatar), PathTo.Combine(ppath, mo.UserPhoto));
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(ex);
                                        }
                                    }
                                }
                                else
                                {
                                    vm.Msg = ruvm.Msg;
                                }
                            }
                            else
                            {
                                vm = ValidateLogin(vtype, vmo);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Apps.FilterConfigs.WriteLog(HttpContext, ex);
                Response.Headers["X-Output-Msg"] = ex.ToJson();
                vm.Set(ex);
            }

            //成功
            if (vm.Code == 200)
            {
                var rurl = Request.Cookies["ReturnUrl"];
                rurl = string.IsNullOrWhiteSpace(rurl) ? "/" : rurl;

                if (rurl.StartsWith("http"))
                {
                    rurl += "?cookie=ok";
                }

                return(Redirect(rurl));
            }
            else
            {
                return(Redirect("/home/error"));
            }
        }