Google OAuth2-渲染同意对话框

时间:2019-07-06 22:37:53

标签: .net-core google-oauth2

根据documentation,我实现了oauth2协议的一部分

获得同意对话框的第一部分。

[HttpPost("login")]
    public async void GoogleLogin()
    {
        try
        {
            _logger.LogInformation("Hello");
            const string state = "1234567890";
            var client = new RestClient("https://accounts.google.com/.well-known/openid-configuration");
            var request = new RestRequest(Method.GET);
            var uri = await client.ExecuteGetTaskAsync(request);

            if (!uri.IsSuccessful)
            {
                _logger.LogError("Try to get well-known resource");
            }

            Console.WriteLine($"Kid: {state}");
            var json = JsonConvert.DeserializeObject<OpenIdConfiguration>(uri.Content);

            var client2 = new RestClient(json.AuthorizationEndpoint);

            var request2 = new RestRequest(Method.GET);
            request2.AddQueryParameter("client_id", Environments.GoogleAuthenticationClientId);
            request2.AddQueryParameter("response_type", "code");
            request2.AddQueryParameter("scope",
                string.Join(",", new List<string> {SheetsService.Scope.Spreadsheets}.ToArray()));
            request2.AddQueryParameter("redirect_uri", "http://localhost:5000/api/auth/authorize");
            request2.AddCookie("state", state);
            var uri2 = await client2.ExecuteGetTaskAsync(request2);

            if (!uri2.IsSuccessful)
            {
                _logger.LogError("Try to auth");
            }

            Redirect(uri2.ResponseUri.AbsoluteUri); // try to do it in that way but don't get the expected result
        }
        catch (Exception ex)
        {
            _logger.LogError(ex.Message);
        }
    }

最后,用户必须批准Google的同意,为此,必须显示同意对话框  for example

当服务器从Google授权获得成功响应时,必须以某种方式显示对话框,这是在python上执行此操作的方式

esponse = make_response(
  render_template('index.html',
                  CLIENT_ID=CLIENT_ID,
                  STATE=state,
                  APPLICATION_NAME=APPLICATION_NAME))

如何使用.net核心在客户端显示对话框?

谢谢迈克尔。

0 个答案:

没有答案