Пример #1
0
        public IActionResult GithubCode([FromQuery] string code, [FromQuery] string state)
        {
            try
            {
                _gHAuth.HasState(state);
                string uri = _gHAuth.GetAccessTokenUri(code, state, out KeyValuePair <string, Guid[]> redirect_uri_user_id_pair);
                _gHAuth.RemoveState(state);
                GHAuthenticatedUserInfo user_info = _gHUser.GetAuthenticatedUserInfo(uri);
                Guid   account_id = _account.Add(user_info, VSCID.Github, out bool is_connected);
                string user = "", jwt = "";

                string error = _account.Connect(redirect_uri_user_id_pair.Value[1], account_id);
                if (error != null)
                {
                    if (string.IsNullOrEmpty(redirect_uri_user_id_pair.Key))
                    {
                        return(BadRequest(error));
                    }
                    return(Redirect(redirect_uri_user_id_pair.Key + (redirect_uri_user_id_pair.Key.Contains("?") ? "&" : "?") + "error=" + error));
                }

                user += "nococid";
                jwt  += _jwtAuth.GenerateJwt(redirect_uri_user_id_pair.Value[0], redirect_uri_user_id_pair.Value[1], ApplicationRole.Web_User);
                if (string.IsNullOrEmpty(redirect_uri_user_id_pair.Key))
                {
                    return(Ok(jwt));
                }
                return(Redirect(redirect_uri_user_id_pair.Key + "?user="******"&jwt=" + jwt));
            }
            catch (Exception e)
            {
                return(GetError(e));
            }
        }
Пример #2
0
        public Guid Add(GHAuthenticatedUserInfo model, Guid vsc_id, out bool is_connected)
        {
            try
            {
                Account account = _account.GetOne(a => a.ThirdPartyAccountId.Trim().Equals(model.User.Id.ToString().Trim()));
                if (account == null)
                {
                    account = _account.Add(new Account
                    {
                        Name = model.User.Login,
                        ThirdPartyAccountId = model.User.Id.ToString(),
                        IsMain = false,
                        ToolId = ToolID.Github
                    });
                }
                account.AccessToken = model.Token.Access_token;
                account.AvatarUrl   = model.User.Avatar_url;
                account.Email       = model.Email;
                SaveChanges();

                is_connected = account.UserId != null;
                return(account.Id);
            }
            catch (Exception e)
            {
                throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while add third party account!",
                                                                         e, DateTime.Now, "Server", "Service_Account_Add");
            }
        }