Microsoft MVP성태의 닷넷 이야기
닷넷: 2260. C# - Google 로그인 연동 (ASP.NET 예제) [링크 복사], [링크+제목 복사],
조회: 9211
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 16  17  [18]  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13490정성태12/19/202310105개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
13489정성태12/19/202310056개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
13488정성태12/18/20239525오류 유형: 884. HTTP 500.0 - 명령행에서 실행한 ASP.NET Core 응용 프로그램을 실행하는 방법
13487정성태12/16/202310633개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행 [1]
13486정성태12/15/20239741개발 환경 구성: 695. Nuget config 파일에 값 설정/삭제 방법
13485정성태12/15/20239426오류 유형: 883. dotnet build/restore - error : Root element is missing
13484정성태12/14/202310164개발 환경 구성: 694. Windows 디렉터리 경로를 WSL의 /mnt 포맷으로 구하는 방법
13483정성태12/14/202310418닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법파일 다운로드1
13482정성태12/13/202311643닷넷: 2183. C# - eFriend Expert OCX 예제를 .NET Core/5+ Console App에서 사용하는 방법 [2]파일 다운로드1
13481정성태12/13/202310540개발 환경 구성: 693. msbuild - .NET Core/5+ 프로젝트에서 resgen을 이용한 리소스 파일 생성 방법파일 다운로드1
13480정성태12/12/202312387개발 환경 구성: 692. Windows WSL 2 + Chrome 웹 브라우저 설치
13479정성태12/11/202310029개발 환경 구성: 691. WSL 2 (Ubuntu) + nginx 환경 설정
13477정성태12/8/202310672닷넷: 2182. C# - .NET 7부터 추가된 Int128, UInt128 [1]파일 다운로드1
13476정성태12/8/202310519닷넷: 2181. C# - .NET 8 JsonStringEnumConverter의 AOT를 위한 개선파일 다운로드1
13475정성태12/7/202310775닷넷: 2180. .NET 8 - 함수 포인터에 대한 Reflection 정보 조회파일 다운로드1
13474정성태12/6/202310355개발 환경 구성: 690. 닷넷 코어/5+ 버전의 ilasm/ildasm 실행 파일 구하는 방법 - 두 번째 이야기
13473정성태12/5/202310698닷넷: 2179. C# - 값 형식(Blittable)을 메모리 복사를 이용해 바이트 배열로 직렬화/역직렬화파일 다운로드1
13472정성태12/4/202310113C/C++: 164. Visual C++ - InterlockedCompareExchange128 사용 방법
13471정성태12/4/202310648Copilot - To enable GitHub Copilot, authorize this extension using GitHub's device flow
13470정성태12/2/202311264닷넷: 2178. C# - .NET 8부터 COM Interop에 대한 자동 소스 코드 생성 도입 [1]파일 다운로드1
13469정성태12/1/202311170닷넷: 2177. C# - (Interop DLL 없이) CoClass를 이용한 COM 개체 생성 방법파일 다운로드1
13468정성태12/1/202310176닷넷: 2176. C# - .NET Core/5+부터 달라진 RCW(Runtime Callable Wrapper) 대응 방식파일 다운로드1
13467정성태11/30/202310933오류 유형: 882. C# - Unhandled exception. System.Runtime.InteropServices.COMException (0x800080A5)파일 다운로드1
13466정성태11/29/202310759닷넷: 2175. C# - DllImport 메서드의 AOT 지원을 위한 LibraryImport 옵션
13465정성태11/28/202310466개발 환경 구성: 689. MSBuild - CopyToOutputDirectory가 "dotnet publish" 시에는 적용되지 않는 문제파일 다운로드1
13464정성태11/28/202310371닷넷: 2174. C# - .NET 7부터 UnmanagedCallersOnly 함수 export 기능을 AOT 빌드에 통합파일 다운로드1
... 16  17  [18]  19  20  21  22  23  24  25  26  27  28  29  30  ...