Ejemplo n.º 1
0
        public ActionResult AuthBind()
        {
            string uid = Func.Common.GetLoginUserInfo().UserId;

            using (var db = new ContextBase())
            {
                var query = from a in db.SysUser
                            join b in db.SysAuthorize on a.SuId equals b.SuId
                            where a.SuId == uid
                            select b;
                var mo = query.FirstOrDefault();
                if (mo == null)
                {
                    mo = new SysAuthorize();
                }
                return(View(mo));
            }
        }
Ejemplo n.º 2
0
        public ActionResult AuthCallback(string code)
        {
            var    result = new AccountValidationVM();
            string vtype  = RouteData.Values["id"]?.ToString().ToLower();

            try
            {
                //唯一标示
                string openId = string.Empty;
                try
                {
                    switch (vtype)
                    {
                    case "qq":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = code
                            });

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

                        //获取 UserInfo
                        var openId_Get_User_Info_ResultEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token,
                                openid       = openId_ResultEntity.openid
                            });

                        //身份唯一标识
                        openId = openId_ResultEntity.openid;
                    }
                    break;

                    case "weibo":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = code
                            });

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

                        //获取 users/show
                        var userShow_ResultEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfo_ResultEntity.uid)
                            });

                        openId = accessToken_ResultEntity.access_token;
                    }
                    break;

                    case "github":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = code
                            });

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

                        openId = user_ResultEntity.id.ToString();
                    }
                    break;

                    case "taobao":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = Taobao.AccessToken(new Taobao_AccessToken_RequestEntity()
                            {
                                code = code
                            });

                        openId = accessToken_ResultEntity.open_uid;
                    }
                    break;

                    case "microsoft":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = code
                            });

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

                        openId = user_ResultEntity.id.ToString();
                    }
                    break;
                    }
                }
                catch (Exception ex)
                {
                    result.message = ex.Message;
                }

                if (string.IsNullOrWhiteSpace(openId))
                {
                    result.message = "身份验证失败";
                }
                else
                {
                    //判断是绑定操作
                    bool isbind = false;
                    if (User.Identity.IsAuthenticated)
                    {
                        var aboa = Request.Cookies["AccountBindOAuth"]?.Value;
                        if (!string.IsNullOrWhiteSpace(aboa) && (DateTime.Now - DateTime.Parse(aboa)).TotalSeconds < 120)
                        {
                            string uid = Func.Common.GetLoginUserInfo().UserId;

                            using (var db = new ContextBase())
                            {
                                var sysauth = db.SysAuthorize.Where(x => x.SuId == uid).FirstOrDefault();
                                var isadd   = sysauth == null;
                                //新增
                                if (isadd)
                                {
                                    sysauth = new SysAuthorize()
                                    {
                                        SaId = Guid.NewGuid().ToString(),
                                        SuId = uid
                                    };
                                }

                                switch (vtype)
                                {
                                case "qq":
                                    sysauth.OpenId1 = openId;
                                    break;

                                case "weibo":
                                    sysauth.OpenId2 = openId;
                                    break;

                                case "github":
                                    sysauth.OpenId3 = openId;
                                    break;

                                case "taobao":
                                    sysauth.OpenId4 = openId;
                                    break;

                                case "microsoft":
                                    sysauth.OpenId5 = openId;
                                    break;
                                }
                                if (isadd)
                                {
                                    db.SysAuthorize.Add(sysauth);
                                }
                                else
                                {
                                    db.SysAuthorize.Update(sysauth, db);
                                }
                                db.SaveChanges();
                            }

                            Response.Cookies.Remove("AccountBindOAuth");
                            isbind = true;

                            result.code    = 301;
                            result.message = "绑定成功";
                            result.url     = "/";
                        }
                    }

                    //非绑定操作
                    if (!isbind)
                    {
                        using (var db = new ContextBase())
                        {
                            SysUser vmo = null;
                            switch (vtype)
                            {
                            case "qq":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId1 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "weibo":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId2 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "github":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId3 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "taobao":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId4 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "microsoft":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId5 == openId
                                       select b).FirstOrDefault();
                                break;
                            }

                            //没关联
                            if (vmo == null)
                            {
                                result.code    = 302;
                                result.message = "未关联账号,先账号密码登录再关联才能使用";
                            }
                            else
                            {
                                result = LoginValidation(vmo, "_pass_", 1);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
            }

            //成功
            if (result.code == 100)
            {
                return(Redirect(result.url));
            }
            else
            {
                return(View(result));
            }
        }