Microsoft MVP성태의 닷넷 이야기
닷넷: 2260. C# - Google 로그인 연동 (ASP.NET 예제) [링크 복사], [링크+제목 복사],
조회: 9190
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)

C# - Google 로그인 연동 (ASP.NET 예제)

이에 대해서는 구글 측에서 자세한 문서를 제공하고 있습니다.

API Client Libraries - .NET
; https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth?hl=ko

다른 OAuth 연동과 마찬가지로 사이트를 위한 인증 키를 발급받아야 하는데요, 아래의 Google Cloud 콘솔 페이지에서,

Google API 콘솔
; https://console.cloud.google.com/?hl=ko

"API 및 서비스" / "사용자 인증 정보"와 "OAuth 동의 화면"을 거쳐 진행할 수 있습니다. 관련해서 다음의 글에서 자세하게 소개하고 있으니 따라 하시면 됩니다.

구글로그인 쉽게 구현하기 1편 - Google Developers 설정
; https://notspoon.tistory.com/45

이후 코드는 아래의 글에서 예제까지 제공하고 있는데요,

OAuth 2.0
; https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth?hl=ko

2개의 예제 코드는 각각 Console Application과 ASP.NET Core를 기준으로 작성돼 있습니다. 만약 그 2가지 유형이라면 해당 예제를 적용해 쉽게 구현할 수 있는데요, 만약 기존의 ASP.NET (.NET Framework) 환경이라면 조금 손을 봐야 합니다.

이대로 끝내면 아쉬우니 ^^ ASP.NET용으로 직접 작성해 볼까요?

우선, ASP.NET (.NET Framework) WebForms 프로젝트를 만들어 nuget 참조를 추가하고,

Install-Package Google.Apis.Auth 

임의의 페이지에 다음과 같이 (독립 실행형 응용 프로그램을 위한 용도의) GoogleWebAuthorizationBroker.AuthorizeAsync 호출을 하면,

/* client_secrets.json 파일의 내용
{
    "web": {
        "client_id": "...[여러분이 받은 ID]...",
        "client_secret": "...[여러분이 받은 Client Secret]...",
    }
}
*/

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = Server.MapPath("~");
            string jsonPath = Path.Combine(path, "bin", "client_secrets.json");
            string[] scopes = new string[] { "https://www.googleapis.com/auth/userinfo.email" };

            using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
            {
                var secrets = GoogleClientSecrets.FromStream(stream).Secrets;

                // async 메서드 내에서는 await 호출을 하겠지만, 이 예제에서는 Result 호출로 동기 처리
                var results = GoogleWebAuthorizationBroker.AuthorizeAsync(
                                secrets, scopes, "user", CancellationToken.None, null).Result;
            }
        }
    }
}

이런 오류 화면으로 redirect 됩니다.

Access blocked: This app's request is invalid

testuser@gmail.com

You can't sign in because this app sent an invalid request. You can try again later, or contact the developer about this issue. Learn more about this error
If you are a developer of this app, see error details.
Error 400: redirect_uri_mismatch

위의 메시지 중 "error details" 링크를 누르면 좀 더 자세한 내용이 나오지만,

Error 400: redirect_uri_mismatch

You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy.

If you're the app developer, register the redirect URI in the Google Cloud Console.
Request details: redirect_uri=http://localhost:58434/authorize/
Related developer documentation

딱히 도움이 되지는 않습니다. redirect_uri_mismatch 오류 상황은 사용자가 (Google Cloud 콘솔 페이지에서) 설정한 redirect 링크와 맞지 않아서 발생할 수 있습니다. 하지만, 그 링크를 설정했는데도 불구하고 (ASP.NET의 경우라면) 저 오류는 여전히 해결이 안 될 텐데요, 왜냐하면 GoogleWebAuthorizationBroker.AuthorizeAsync API 자체가 인증을 위한 요청 URL에 redirect_uri 파트를 json 파일의 내용으로부터 가져오지 않기 때문입니다. (위의 오류 메시지에 "http://localhost:58434/authorize/"로 설정했다고 나옵니다.)

실제로 AuthorizeAsync의 첫 번째 인자에 들어가는 secrets는 ClientSecrets 타입으로,

namespace Google.Apis.Auth.OAuth2
{
    public sealed class ClientSecrets
    {
        [Newtonsoft.Json.JsonProperty("client_id")]
        public string ClientId { get; set; }

        [Newtonsoft.Json.JsonProperty("client_secret")]
        public string ClientSecret { get; set; }
    }
}

redirect uri 정보가 없는 client_id, client_secret만 조합해서 인증 요청을 시도할 뿐입니다.

따라서, .NET Framework 환경의 ASP.NET 환경에서는 인증 요청에 대한 URL을 직접 작성해야 하는데요, 다행히 이미 Google.Apis.Auth
패키지에는 GoogleAuthorizationCodeFlow 타입이 이런 과정을 자동화합니다. 아래의 코드를 보면, redirect URI 정보까지 모두 포함시켜 GoogleAuthorizationCodeFlow.CreateAuthorizationCodeRequest 메서드를 호출해 인증 요청을 위한 URL을 구하는 것을 보여줍니다.

protected void Page_Load(object sender, EventArgs e)
{
    string path = Server.MapPath("~");
    string jsonPath = Path.Combine(path, "bin", "client_secrets.json");
    string[] scopes = new string[] { "https://www.googleapis.com/auth/userinfo.email" };

    using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
    {
        var clientSecrets = GoogleClientSecrets.FromStream(stream).Secrets;
        var initializer = new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = clientSecrets,
            Scopes = scopes
        };
        var googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow(initializer);
        var request = googleAuthorizationCodeFlow.CreateAuthorizationCodeRequest(
            "http://localhost:53745/loginProcess.aspx"); // redirect uri 설정
        Uri uri = request.Build();

        string authRequestUrl = uri.ToString();
        Response.Redirect(authRequestUrl);
    }
}

authRequestUrl에 들어가는 값은 대충 이런 구조입니다.

https://accounts.google.com/o/oauth2/v2/auth?
    access_type=offline&
    response_type=code&
    client_id=...[oauth client id]...&
    redirect_uri=http%3A%2F%2F127.0.0.1%3A53745%2FloginProcess.aspx&
    scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile

원하는 값들이 잘 들어가 있습니다. 마지막의 Response.Redirect는 그렇게 작성한 URL로 사용자 웹 브라우저 화면을 전환시켜 Google 인증 페이지로 넘어갈 수 있게 합니다.

이후, 사용자가 구글 인증을 마치면 CreateAuthorizationCodeRequest 메서드로 넘겨준 redirect URL, 위의 경우 "http://127.0.0.1:53745/loginProcess.aspx"로 다시 rediect가 발생합니다.

그럼 loginProcess.aspx에는 대충 다음과 같은 식으로 요청이 들어오고, 이 정보로부터 Token을 가져오는 작업을 진행하면 되는데요,

/loginProcess?
    code=...[access token을 구하기 위한 값]...&
    scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&
    authuser=0&
    prompt=consent

여기서 쓸모 있는 것은 code 하나입니다. 이 값을 이용해 GoogleAuthorizationCodeFlow.ExchangeCodeForTokenAsync API를 다시 호출하면 TokenResponse를 구할 수 있습니다.

public partial class loginProcess : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string code = Request.QueryString["code"];
        string path = Server.MapPath("~");
        string jsonPath = Path.Combine(path, "bin", "client_secrets.json");

        using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
        {
            var clientSecrets = GoogleClientSecrets.FromStream(stream).Secrets;
            var initializer = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = clientSecrets,
            };

            var flow = new GoogleAuthorizationCodeFlow(initializer);
            
            string rediect_uri = "http://localhost:53745/loginProcess.aspx";
            var tokenResponse = flow.ExchangeCodeForTokenAsync(null, code,
                rediect_uri, CancellationToken.None).Result;

            string accessToken = tokenResponse.AccessToken;
        }
    }
}

TokenResponse에는 access_token, token_type,... 등의 값이 있지만 이전에 CreateAuthorizationCodeRequest를 요청했을 시 지정했던 "Scope"에 해당하는 정보까지 포함한 IdToken 값을 JWT(Json Web Token) 인코딩된 유형으로 가지고 있습니다.

따라서, IdToken을 JWT 디코딩하면 되는데요, 이런 목적으로 GoogleJsonWebSignature.ValidateAsync 메서드를 호출할 수 있습니다.

var idToken = GoogleJsonWebSignature.ValidateAsync(tokenResponse.IdToken).Result;

string email = idToken.Email;
string name = idToken.Name;
string first_name = idToken.FamilyName;
string last_name = idToken.GivenName;
string id = idToken.Subject;

제가 운영하는 sysnet 개인 블로그처럼 단순히 로그인 처리만 하는 경우라면 accessToken을 보관할 필요는 없습니다. 필요한 것은, 사용자를 구분하기 위한 idToken.Subject 값 정도인데요, 따라서 이것을 FormsAuthentication.SetAuthCookie로 지정해 로그인했다는 기록만을 남기면 됩니다.

FormsAuthentication.SetAuthCookie(id, true);




부가적으로, 인증을 위한 redirect 중에 유지할 필요가 있는 정보가 있다면 (Facebook 인증에서도 동일했던) state를 설정할 수 있습니다.

RFC 6749 - The OAuth 2.0 Authorization Framework
 - 4.1.1.  Authorization Request
; https://www.rfc-editor.org/rfc/rfc6749#section-4.1.1

예를 들어, 위의 예제에서 loginProcess.aspx 내부의 코드를 거친 후, (로그인을 시도하기 전에) 사용자가 방문했던 그 페이지를 보여주는 것이 더 자연스러울 것입니다.

따라서 이런 경우에는 그 페이지 주소, 또는 그 페이지로 갈 수 있는 정보를 state 쿼리 인자로 넘겨주면 되는데요,

var googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow(initializer);
var request = googleAuthorizationCodeFlow.CreateAuthorizationCodeRequest(
    "http://localhost:53745/loginProcess.aspx" // redirect_uri에는 부가적인 매개 변수를 넘길 수 없으므로,
    );
Uri uri = request.Build();

string authRequestUrl = uri.ToString();
Response.Redirect(authRequestUrl + "&state=test1 test2"); // 별도의 state 매개 변수로 넘겨줍니다.

또는, Google.Apis.Auth를 사용하는 위의 예제에서는 GoogleAuthorizationCodeFlow.Initializer에서 UserDefinedQueryParams를 통해 전달하는 것도 가능합니다.

var clientSecrets = GoogleClientSecrets.FromStream(stream).Secrets;
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = clientSecrets,
    Scopes = scopes,
    UserDefinedQueryParams = new Dictionary<string, string>
    {
        ["state"] = "test1 test2",
    }
};

var googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow(initializer);
var request = googleAuthorizationCodeFlow.CreateAuthorizationCodeRequest(
    "http://localhost:53745/loginProcess.aspx"
    );
Uri uri = request.Build(); // 이 시점에 "state=test1 test2"가 함께 URL에 포함됨

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




그런데 이상하군요, 위의 예제를 다른 컴퓨터에서, 동일하게 Windows 11 + Edge + Visual Studio로 실행했는데, 한 컴퓨터에서만 구글 로그인 이후 다음과 같은 오류가 발생합니다.

Couldn't sign you in

This browser or app may not be secure. Learn more
Try using a different browser. If you're already using a supported browser, you can try again to sign in.

반면 Chrome 브라우저를 띄워 "localhost:53745"로 접속해 테스트를 했더니 정상적으로 로그인이 됩니다. 이유는, "Enable JavaScript debugging for ASP.NET (Chrome, Edge and IE)" 설정이 켜져 있었던 탓에,

Visual Studio가 node.exe를 경유해 Edge.exe를 띄우는 경우
; https://www.sysnet.pe.kr/2/0/13620

제약이 많은 edge 브라우저가 실행돼 "This browser or app may not be secure"라는 오류가 발생한 것입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/11/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13917정성태4/30/202544VS.NET IDE: 199. Directory.Build.props에 정의한 속성에 대해 Condition 제약으로 값을 변경하는 방법
13916정성태4/23/2025446디버깅 기술: 221. WinDbg 분석 사례 - ASP.NET HttpCookieCollection을 다중 스레드에서 사용할 경우 무한 루프 현상 - 두 번째 이야기
13915정성태4/13/20251652닷넷: 2331. C# - 실행 시에 메서드 가로채기 (.NET 9)파일 다운로드1
13914정성태4/11/20251968디버깅 기술: 220. windbg 분석 사례 - x86 ASP.NET 웹 응용 프로그램의 CPU 100% 현상 (4)
13913정성태4/10/20251204오류 유형: 950. Process Explorer - 64비트 윈도우에서 32비트 프로세스의 덤프를 뜰 때 "Error writing dump file: Access is denied." 오류
13912정성태4/9/2025863닷넷: 2330. C# - 실행 시에 메서드 가로채기 (.NET 5 ~ .NET 8)파일 다운로드1
13911정성태4/8/20251091오류 유형: 949. WinDbg - .NET Core/5+ 응용 프로그램 디버깅 시 sos 확장을 자동으로 로드하지 못하는 문제
13910정성태4/8/20251257디버깅 기술: 219. WinDbg - 명령어 내에서 환경 변수 사용법
13909정성태4/7/20251725닷넷: 2329. C# - 실행 시에 메서드 가로채기 (.NET Framework 4.8)파일 다운로드1
13908정성태4/2/20252138닷넷: 2328. C# - MailKit: SMTP, POP3, IMAP 지원 라이브러리
13907정성태3/29/20251933VS.NET IDE: 198. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C# 프로젝트의 출력 경로 변경하기
13906정성태3/27/20252190닷넷: 2327. C# - 초기화되지 않은 메모리에 접근하는 버그?파일 다운로드1
13905정성태3/26/20252220Windows: 281. C++ - Windows / Critical Section의 안정화를 위해 도입된 "Keyed Event"파일 다운로드1
13904정성태3/25/20251902디버깅 기술: 218. Windbg로 살펴보는 Win32 Critical Section파일 다운로드1
13903정성태3/24/20251522VS.NET IDE: 197. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C++ 프로젝트의 출력 경로 변경하기
13902정성태3/24/20251737개발 환경 구성: 742. Oracle - 테스트용 hr 계정 및 데이터 생성파일 다운로드1
13901정성태3/9/20252112Windows: 280. Hyper-V의 3가지 Thread Scheduler (Classic, Core, Root)
13900정성태3/8/20252346스크립트: 72. 파이썬 - SQLAlchemy + oracledb 연동
13899정성태3/7/20251806스크립트: 71. 파이썬 - asyncio의 ContextVar 전달
13898정성태3/5/20252124오류 유형: 948. Visual Studio - Proxy Authentication Required: dotnetfeed.blob.core.windows.net
13897정성태3/5/20252367닷넷: 2326. C# - PowerShell과 연동하는 방법 (두 번째 이야기)파일 다운로드1
13896정성태3/5/20252178Windows: 279. Hyper-V Manager - VM 목록의 CPU Usage 항목이 항상 0%로 나오는 문제
13895정성태3/4/20252220Linux: 117. eBPF / bpf2go - Map에 추가된 요소의 개수를 확인하는 방법
13894정성태2/28/20252250Linux: 116. eBPF / bpf2go - BTF Style Maps 정의 구문과 데이터 정렬 문제
13893정성태2/27/20252197Linux: 115. eBPF (bpf2go) - ARRAY / HASH map 기본 사용법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...