public IActionResult GitHub()
        {
            var res = _gitHubLogin.Authorize();

            if (res != null && res?.Code == Code.Success)
            {
                //成功逻辑
                return(Content(res.Result.ToString()));
            }
            return(View());
        }
Пример #2
0
        public JsonResult GitHubLogin(string?code)
        {
            if (SendCacheStore.GetSystemValue("githubEnable") != "true")
            {
                return(Fail(401, "未启用GITHUB登陆"));
            }

            if (UserName != null && Token != null)
            {
                var direct = string.Format("/#login?token={0}", Token);
                HttpContext.Response.Redirect(direct, true);
                return(OK());
            }
            else
            {
                if (string.IsNullOrEmpty(code))
                {
                    return(OK(m_gitHubLogin.GetOauthUrl()));
                }
                else
                {
                    var res = m_gitHubLogin.Authorize();
                    if (res != null && res.Result != null && res.Code == Code.Success)
                    {
                        string?githubUserName = null;
                        string?avtar          = null;
                        string email          = "";
                        if (res.Result.TryGetValue("login", out JToken? jToken))
                        {
                            githubUserName = jToken.ToString();
                        }

                        if (res.Result.TryGetValue("avatar_url", out jToken))
                        {
                            avtar = jToken.ToString();
                        }

                        if (res.Result.TryGetValue("email", out jToken))
                        {
                            email = jToken.ToString();
                        }

                        if (githubUserName != null && avtar != null)
                        {
                            SendUserInfo user;
                            if (DBManager.Instance.IsUser(githubUserName))
                            {
                                user        = DBManager.Instance.DBase.Query <SendUserInfo>().FirstOrDefault(e => e.UserName == githubUserName);
                                user.Avatar = avtar;
                                DBManager.Instance.DBase.Update(user);
                                if (!user.Active)
                                {
                                    return(Fail(401, "用户被禁用"));
                                }
                            }
                            else
                            {
                                user = new SendUserInfo()
                                {
                                    UserName   = githubUserName,
                                    Avatar     = avtar,
                                    Password   = "******".ToMd5(),
                                    Email      = email,
                                    Active     = true,
                                    Token      = Guid.NewGuid().ToString("N").ToUpper(),
                                    CreateTime = DateTime.Now
                                };
                                DBManager.Instance.DBase.Insert(user);
                            }

                            var token  = GenToken(user.UserName);
                            var direct = string.Format("/#login?token={0}", token);
                            HttpContext.Response.Redirect(direct, true);
                            return(OK(user));
                        }
                    }
                }
            }
            return(Fail(401, "Github登陆失败"));
        }