﻿
{"id":25280,"date":"2022-06-25T11:08:03","date_gmt":"2022-06-25T11:08:03","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=25280"},"modified":"2022-06-25T11:12:33","modified_gmt":"2022-06-25T11:12:33","slug":"asp-net-core-identity-angular-14-esliginde-google-login","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-core-identity-angular-14-esliginde-google-login\/","title":{"rendered":"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde, Angular ile geli\u015ftirdi\u011fimiz uygulamalarda kullan\u0131c\u0131lar\u0131n sosyal profillerini kullanarak giri\u015f yapmak istemeleri durumlar\u0131na istinaden Google hesab\u0131 \u00fczerinden bir oturum a\u00e7ma \u00e7al\u0131\u015fmas\u0131 sergiliyor olaca\u011f\u0131z. Tabi bu \u00e7al\u0131\u015fmada, Angular k\u0131sm\u0131nda <a href=\"https:\/\/github.com\/abacritt\/angularx-social-login\" rel=\"noopener\" target=\"_blank\">@abacritt\/angularx-social-login<\/a> k\u00fct\u00fcphanesinden istifade edecek ve sosyal a\u011flardan gelen kay\u0131t ve oturum bilgilerini tutabilmek ve y\u00f6netebilmek i\u00e7in backend&#8217;de Asp.NET Core Identity mekanizmas\u0131n\u0131 kullan\u0131yor olaca\u011f\u0131z.<\/p>\n<h3 style=\"color:#e83e8c;\">Ba\u015flarken<\/h3>\n<p>Her \u015feyden \u00f6nce hali haz\u0131rda temelleri at\u0131lm\u0131\u015f Angular 14 ve Asp.NET Core Web API uygulamalar\u0131 olu\u015fturunuz ve \u00f6zellikle Asp.NET Core uygulamas\u0131nda <a href=\"https:\/\/www.gencayyildiz.com\/blog\/asp-net-core-identity-yazi-dizisi\/\" rel=\"noopener\" target=\"_blank\">Identity<\/a> konfig\u00fcrasyonlar\u0131n\u0131 ve hatta <a href=\"https:\/\/www.gencayyildiz.com\/blog\/asp-net-core-3-1-ile-token-bazli-kimlik-dogrulamasi-ve-refresh-token-kullanimijwt\/\" rel=\"noopener\" target=\"_blank\">JWT<\/a> gibi authentication yap\u0131lanmalar\u0131n\u0131 tamamlay\u0131n\u0131z.<\/p>\n<h3 style=\"color:#e83e8c;\">Google Platformunda Credentials Olu\u015fturma<\/h3>\n<p>\u0130lk olarak oturum a\u00e7ma i\u015flemini ger\u00e7ekle\u015ftirece\u011fimiz Google API platformu \u00fczerinden bir kimlik bilgisi olu\u015fturarak i\u015fe ba\u015flamam\u0131z gerekmektedir. Bunun i\u00e7in <a href=\"https:\/\/console.cloud.google.com\/projectcreate?previousPage=%2Fwelcome%3ForganizationId%3D0&#038;organizationId=0\" rel=\"noopener\" target=\"_blank\">New Project &#8211; Google Cloud Console<\/a> sayfas\u0131 \u00fczerinden uygulaman\u0131z i\u00e7in Google Cloud&#8217;da bir proje olu\u015fturunuz.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"525\" height=\"435\" class=\"aligncenter size-full wp-image-25286\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login.png 525w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-300x249.png 300w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/a>Ard\u0131ndan <a href=\"https:\/\/console.cloud.google.com\/\" rel=\"noopener\" target=\"_blank\">console.cloud.google.com<\/a> sayfas\u0131na gelerek a\u015fa\u011f\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi biraz \u00f6nce olu\u015fturulan projenin se\u00e7ili oldu\u011fundan emin olacak \u015fekilde &#8216;Quick access&#8217; alt\u0131ndaki i\u015faretli k\u0131sma t\u0131klay\u0131n\u0131z.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-1.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"1210\" height=\"717\" class=\"aligncenter size-full wp-image-25287\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-1.png 1210w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-1-300x178.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-1-1024x607.png 1024w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-1-768x455.png 768w\" sizes=\"auto, (max-width: 1210px) 100vw, 1210px\" \/><\/a>\u015eimdi a\u00e7\u0131lan sayfada uygulaman\u0131z i\u00e7in bir yap\u0131land\u0131rma olu\u015fturacaks\u0131n\u0131z. Bu yap\u0131land\u0131rma i\u00e7in a\u015fa\u011f\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde vurguland\u0131\u011f\u0131 gibi sol men\u00fcden &#8216;Credentials&#8217; sekmesine geliniz ve ard\u0131ndan &#8216;Create Credentials&#8217; butonuna oradan da a\u00e7\u0131lan pencereden &#8216;OAuth client ID&#8217; sekmesine t\u0131klay\u0131n\u0131z.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-2.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"638\" height=\"325\" class=\"aligncenter size-full wp-image-25289\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-2.png 638w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-2-300x153.png 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/a>A\u00e7\u0131lan sayfada izin ekran\u0131n\u0131 yap\u0131land\u0131rabilmek i\u00e7in sa\u011f taraftaki &#8216;Configure Consent Screen&#8217; butonuna t\u0131klay\u0131n\u0131z.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"1628\" height=\"197\" class=\"aligncenter size-full wp-image-25291\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3.png 1628w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3-300x36.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3-1024x124.png 1024w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3-768x93.png 768w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-3-1536x186.png 1536w\" sizes=\"auto, (max-width: 1628px) 100vw, 1628px\" \/><\/a>Ard\u0131ndan a\u00e7\u0131lan sayfada a\u015fa\u011f\u0131daki gibi &#8216;External&#8217;i se\u00e7erek ilerleyiniz.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-4.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"442\" height=\"447\" class=\"aligncenter size-full wp-image-25292\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-4.png 442w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-4-297x300.png 297w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-4-80x80.png 80w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/a>Devam\u0131nda uygulama bilgilerine dair gelen sayfada a\u015fa\u011f\u0131daki gibi basit\u00e7e alanlar\u0131 doldurmak \u015fimdilik yeterli olacakt\u0131r.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-5.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-5.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"454\" height=\"867\" class=\"aligncenter size-full wp-image-25293\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-5.png 454w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-5-157x300.png 157w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/a>Bundan sonra t\u00fcm sayfalar\u0131 h\u0131zl\u0131ca ge\u00e7ebilirsiniz.<\/p>\n<p>Art\u0131k tekrardan &#8216;Credentials&#8217; sekmesine gelip &#8216;Create Credentials&#8217; butonu \u00fczerinden &#8216;OAuth client ID&#8217; sekmesine t\u0131klarsan\u0131z art\u0131k bir &#8216;OAuth client ID&#8217; tan\u0131mlama f\u0131rsat\u0131 elde edebileceksiniz. Evet, biliyorum. Google Cloud bu konuda olduk\u00e7a zahmetli ve yorucu \ud83d\ude42 Ama el mahkum, bu a\u015famalar\u0131n bir \u015fekilde ge\u00e7ilmesi gerekmektedir&#8230; Velhas\u0131l, &#8216;OAuth client ID&#8217; tan\u0131mlarken a\u015fa\u011f\u0131daki g\u00f6rselde oldu\u011fu gibi ad\u0131 ve yetkilendirilmi\u015f javascript origins&#8217;leri bildirmemiz gerekmektedir.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-6.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-6.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"480\" height=\"820\" class=\"aligncenter size-full wp-image-25295\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-6.png 480w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-6-176x300.png 176w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a>Burada bizler Angular uygulamas\u0131n\u0131 localhost&#8217;ta aya\u011fa kald\u0131raca\u011f\u0131m\u0131zdan dolay\u0131 &#8216;http:\/\/localhost:4200&#8217; adresiyle birlikte ayr\u0131ca dikkat ederseniz portsuz hali de bildirilmi\u015ftir. Bu durumun gereklili\u011fi \u00f6nceden edindi\u011fimiz tecr\u00fcbelerle sabit olan bir durumdur. O y\u00fczden sizlerde e\u011fer local&#8217;de \u00e7al\u0131\u015fma sergiliyorsan\u0131z, Google Cloud \u00fczerinde tan\u0131mlama yaparken t\u0131pk\u0131 buradaki gibi port&#8217;suz bir bildiride de bulunmay\u0131 ihmal etmeyiniz. Bu tan\u0131mlamalardan sonra &#8216;Create&#8217; neticesinde Google API sizlere a\u015fa\u011f\u0131daki g\u00f6rselde oldu\u011fu gibi client id ve client secret keyleri verecektir.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-7.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-7.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"430\" height=\"160\" class=\"aligncenter size-full wp-image-25296\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-7.png 430w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-7-300x112.png 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/a>\u0130\u015fte bizler t\u00fcm \u00e7al\u0131\u015fmalar\u0131 bu keyler \u00fczerinden ger\u00e7ekle\u015ftiriyor olaca\u011f\u0131z.<\/p>\n<h3 style=\"color:#e83e8c;\">Angularx-Social-Login Paketinin Y\u00fcklenmesi<\/h3>\n<p>Angular uygulamas\u0131nda; Google login, Facebook login vs. gibi sosyal sitelerden yetkilendirme i\u015flemleri s\u00f6z konusu oldu\u011fu taktirde bizlere olduk\u00e7a yard\u0131m\u0131 dokunacak olan <a href=\"https:\/\/github.com\/abacritt\/angularx-social-login\" rel=\"noopener\" target=\"_blank\">angularx-social-login<\/a> k\u00fct\u00fcphanesinden istifade edece\u011fimizi s\u00f6ylemi\u015ftik. \u015eimdi bu paketi <code style=\"color:red;\"><strong>npm i @abacritt\/angularx-social-login<\/strong><\/code> talimat\u0131 e\u015fli\u011finde Angular uygulaman\u0131za y\u00fckleyiniz ve a\u015fa\u011f\u0131daki konfig\u00fcrasyonlar\u0131 ger\u00e7ekle\u015ftiriniz<span style=\"font-size:12px;\">(Not : \u0130lgili paketi verilen talimat e\u015fli\u011finde y\u00fckleyebilmek i\u00e7in Angular uygulamas\u0131n\u0131n en d\u00fc\u015f\u00fck 14 versiyonunda olmas\u0131 gerekmektedir)<\/span><\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { GoogleLoginProvider, SocialAuthServiceConfig, SocialLoginModule } from '@abacritt\/angularx-social-login';\r\nimport { NgModule } from '@angular\/core';\r\nimport { BrowserModule } from '@angular\/platform-browser';\r\n\r\nimport { AppRoutingModule } from '.\/app-routing.module';\r\nimport { AppComponent } from '.\/app.component';\r\n\r\n@NgModule({\r\n  declarations: &#x5B;\r\n    AppComponent\r\n  ],\r\n  imports: &#x5B;\r\n    BrowserModule,\r\n    AppRoutingModule,\r\n    SocialLoginModule\r\n  ],\r\n  providers: &#x5B;\r\n    {\r\n      provide: &quot;SocialAuthServiceConfig&quot;,\r\n      useValue: {\r\n        autoLogin: false,\r\n        providers: &#x5B;\r\n          {\r\n            id: GoogleLoginProvider.PROVIDER_ID,\r\n            provider: new GoogleLoginProvider(&quot;902986185803-4dl068flq4g27bpj299khhlq7es3g988.apps.googleusercontent.com&quot;)\r\n          }\r\n        ],\r\n        onError: err =&gt; console.log(err)\r\n      } as SocialAuthServiceConfig\r\n    }\r\n  ],\r\n  bootstrap: &#x5B;AppComponent]\r\n})\r\nexport class AppModule { }\r\n<\/pre>\n<\/div>\n<p>Dikkat ederseniz client id de\u011feri burada provider olarak bildirilmektedir. Bu konfig\u00fcrasyonlardan sonra &#8216;login.component.ts&#8217; dosyas\u0131na geliniz ve a\u015fa\u011f\u0131daki gibi &#8216;SocialAuthService&#8217;i \u00e7a\u011f\u0131rarak i\u015flemleri ger\u00e7ekle\u015ftiriniz.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { SocialAuthService, SocialUser } from '@abacritt\/angularx-social-login';\r\nimport { Component, OnInit } from '@angular\/core';\r\n\r\n@Component({\r\n  selector: 'app-login',\r\n  templateUrl: '.\/login.component.html',\r\n  styleUrls: &#x5B;'.\/login.component.scss']\r\n})\r\nexport class LoginComponent implements OnInit {\r\n\r\n  constructor(private socialAuthService: SocialAuthService) {\r\n    this.socialAuthService.authState.subscribe((user: SocialUser) =&gt; {\r\n      console.log(user);\r\n    });\r\n  }\r\n\r\n  ngOnInit(): void {\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<p>Tabi, bir yandan da Google login butonunu \u00e7\u0131karabilmek i\u00e7in ise &#8216;login.component.html&#8217; dosyas\u0131nda a\u015fa\u011f\u0131daki gibi &#8216;asl-google-signin-button&#8217; direktifini \u00e7a\u011f\u0131rman\u0131z gerekmektedir.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;asl-google-signin-button&gt;&lt;\/asl-google-signin-button&gt;\r\n<\/pre>\n<\/div>\n<p>T\u00fcm bu i\u015flemlerden sonra ilgili butona t\u0131klad\u0131\u011f\u0131n\u0131zda a\u015fa\u011f\u0131daki ekran g\u00f6r\u00fcnt\u00fcs\u00fcnde oldu\u011fu gibi Google a\u00e7\u0131s\u0131ndan kullan\u0131c\u0131 taraf\u0131ndan gerekli yetkilendirme sa\u011flanmas\u0131 i\u00e7in bir onay penceresi \u00e7\u0131kacakt\u0131r.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-8.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-8.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"488\" height=\"414\" class=\"aligncenter size-full wp-image-25299\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-8.png 488w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-8-300x255.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/a>E\u011fer kullan\u0131c\u0131 yetkilendirmeyi sa\u011flarsa Google&#8217;dan gelecek olan kullan\u0131c\u0131 bilgileri a\u015fa\u011f\u0131daki gibi olacakt\u0131r.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-9.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-9.png\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"372\" height=\"159\" class=\"aligncenter size-full wp-image-25300\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-9.png 372w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login-9-300x128.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a>B\u00f6ylece \u00e7al\u0131\u015fmam\u0131z\u0131n ilk safhas\u0131 bitmi\u015ftir diyebiliriz. Nihayetinde Angular k\u0131sm\u0131nda kullan\u0131c\u0131 Google login ger\u00e7ekle\u015ftirmekte ve uygulama a\u00e7\u0131s\u0131ndan gerekli veriler elde edilmektedir. \u015eimdi s\u0131ra Asp.NET Core Identity k\u00fct\u00fcphanesiyle bu verileri i\u015flemeye gelmi\u015ftir.<\/p>\n<h3 style=\"color:#e83e8c;\">Angular&#8217;da Ger\u00e7ekle\u015ftirilen Login&#8217;i Asp.NET Core Identity \u0130le Backend&#8217;de de Ger\u00e7ekle\u015ftirmek<\/h3>\n<p>Google&#8217;dan gelen <code>idToken<\/code>&#8216;\u0131 backend&#8217;de de do\u011frulamak i\u00e7in <a href=\"https:\/\/www.nuget.org\/packages\/Google.Apis.Auth\/\" rel=\"noopener\" target=\"_blank\">Google.Apis.Auth<\/a> k\u00fct\u00fcphanesinden istifade ediyor olaca\u011f\u0131z. Haliyle ilgili k\u00fct\u00fcphaneyi verilen adres \u00fczerinden projenize y\u00fckleyiniz ve a\u015fa\u011f\u0131daki geli\u015ftirmeyi ger\u00e7ekle\u015ftiriniz.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class GoogleIdTokenValidationService : IGoogleIdTokenValidationService\r\n    {\r\n        readonly IConfiguration _configuration;\r\n        readonly UserManager&lt;AppUser&gt; _userManager;\r\n        readonly ITokenHandler _tokenHandler;\r\n        public GoogleIdTokenValidationService(\r\n            IConfiguration configuration,\r\n            UserManager&lt;AppUser&gt; userManager,\r\n            ITokenHandler tokenHandler)\r\n        {\r\n            _configuration = configuration;\r\n            _userManager = userManager;\r\n            _tokenHandler = tokenHandler;\r\n        }\r\n\r\n        public async Task&lt;Token&gt; ValidateIdTokenAsync(GoogleLoginVM model)\r\n        {\r\n            ValidationSettings? settings = new GoogleJsonWebSignature.ValidationSettings()\r\n            {\r\n                Audience = new List&lt;string&gt;()\r\n                    { _configuration&#x5B;&quot;ExternalLogin:Google-Client-Id&quot;] }\r\n            };\r\n            Payload payload = await GoogleJsonWebSignature.ValidateAsync(model.IdToken, settings);\r\n\r\n            UserLoginInfo userLoginInfo = new(model.Provider, payload.Subject, model.Provider);\r\n            AppUser user = await _userManager.FindByLoginAsync(userLoginInfo.LoginProvider, userLoginInfo.ProviderKey);\r\n            bool result = user != null;\r\n            if (user == null)\r\n            {\r\n                user = await _userManager.FindByEmailAsync(payload.Email);\r\n                if (user == null)\r\n                {\r\n                    user = new() { Id = Guid.NewGuid().ToString(), Email = payload.Email, UserName = payload.Email, Provider = model.Provider };\r\n                    IdentityResult createResult = await _userManager.CreateAsync(user);\r\n                    result = createResult.Succeeded;\r\n                }\r\n            }\r\n\r\n            if (result)\r\n                await _userManager.AddLoginAsync(user, userLoginInfo);\r\n            else\r\n                throw new Exception(&quot;Invalid external authentication.&quot;);\r\n\r\n            Token token = _tokenHandler.CreateAccessToken(5);\r\n            return token;\r\n        }\r\n    }\r\n<\/pre>\n<\/div>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer; <strong>18<\/strong> ile <strong>23.<\/strong> sat\u0131r aral\u0131\u011f\u0131nda Google Client Id&#8217;yi(appsettings.json&#8217;dan geliyor) tutan bir &#8216;ValidationSettings&#8217; nesnesi ayarlanmakta ve client&#8217;tan gelen &#8216;idToken&#8217; bilgisi e\u015fli\u011finde bu token&#8217;\u0131n payload&#8217;\u0131 ayr\u0131\u015ft\u0131r\u0131lmaktad\u0131r. <strong>25.<\/strong> sat\u0131rda ise d\u0131\u015f kaynaktan gelen kullan\u0131c\u0131 bilgilerini &#8216;AspNetUserLogins&#8217; tablosuna kaydetmemizi sa\u011flayacak olan bir &#8216;UserLoginInfo&#8217; nesnesi olu\u015fturulmaktad\u0131r. <strong>26.<\/strong> sat\u0131rda <code>FindByLoginAsync<\/code> fonksiyonu ile &#8216;AspNetUserLogins&#8217; tablosunda &#8216;UserLoginInfo&#8217; nesnesindeki bilgilere kar\u015f\u0131l\u0131k bir kay\u0131t olup olmad\u0131\u011f\u0131 kontrol edilmekte ve kay\u0131t varsa e\u011fer giri\u015f yapt\u0131r\u0131lmaktad\u0131r. Nihayetinde burada kullan\u0131c\u0131 \u00f6nceden ayn\u0131 d\u0131\u015f kaynaktan geldiyse uygulama taraf\u0131ndan tan\u0131nmas\u0131 ve direkt giri\u015f yapt\u0131r\u0131lmas\u0131 gerekmektedir. <strong>27<\/strong> ile <strong>37.<\/strong> sat\u0131r aral\u0131\u011f\u0131nda ise d\u0131\u015f kaynaktan gelen kullan\u0131c\u0131n\u0131n \u00f6nceden gelmedi\u011fine dair &#8216;AspNetUserLogins&#8217; tablosunda bir kayd\u0131n olmamas\u0131 durumu g\u00f6z \u00f6n\u00fcne al\u0131narak, \u00f6nce bu kullan\u0131c\u0131 email&#8217;inde bir kullan\u0131c\u0131 olup olmad\u0131\u011f\u0131 de\u011ferlendirilmekte, yoksa bu kullan\u0131c\u0131n\u0131n kayd\u0131 ger\u00e7ekle\u015ftirilmektedir. <strong>40.<\/strong> sat\u0131rda ise <code>AddLoginAsync<\/code> fonksiyonu ile d\u0131\u015f kaynaktan giri\u015f yapan kullan\u0131c\u0131n\u0131n bilgileri e\u011fer &#8216;AspNetUserLogins&#8217; tablosunda yoksa i\u015flenmektedir. <strong>44<\/strong> ile <strong>45.<\/strong> sat\u0131r aral\u0131\u011f\u0131nda ise art\u0131k Google&#8217;dan gelen kullan\u0131c\u0131 do\u011fruland\u0131ysa yetkilendirmeyi sa\u011flayacak manevra ger\u00e7ekle\u015ftirilmektedir. Tabi biz burada JWT \u00fcretiyor ve g\u00f6nderiyoruz ama sizler farkl\u0131 bir yetkilendirme davran\u0131\u015f\u0131 neticesinde client&#8217;a d\u00f6n\u00fc\u015f yapabilirsiniz.<\/p>\n<p>Ayr\u0131ca <strong>33.<\/strong> sat\u0131ra bakarsan\u0131z gelen login talebinde eklenecek olan kullan\u0131c\u0131n\u0131n hangi provider e\u015fli\u011finde eklendi\u011finin de kayd\u0131n\u0131 tutmaktay\u0131z. Ne de olsa t\u00fcm kullan\u0131c\u0131lar &#8216;AspNetUsers&#8217; tablosunda tutulacakt\u0131rlar ve hangisinin nereden geldi\u011fini bilebilmek i\u00e7in kullan\u0131c\u0131 seviyesinde bu \u015fekilde bir bilgi tutulmas\u0131 i\u015fimizi kolayla\u015ft\u0131rabilir. Ha e\u011fer ki sizler bu bilgiyi &#8216;AspNetUsers&#8217; tablosunda tutmuyorsan\u0131z &#8216;AspNetUserLogins&#8217; tablosunda hangi kullan\u0131c\u0131n\u0131n hangi provider ile geldi\u011fini inner join e\u015fli\u011finde rahatl\u0131kla \u00f6\u011frenebilirsiniz.(Ki do\u011frusu bu ikincisidir, \u00e7\u00fcnk\u00fc bu makale i\u00e7in g\u00f6rsel a\u00e7\u0131dan g\u00f6rebilelim diye provider bilgisini &#8216;AspNetUsers&#8217; tablosunda tutuyorum ama bu durumda da her bir kullan\u0131c\u0131 i\u00e7in veri tekrar\u0131 s\u00f6z konusu olacak ve bu da normalizasyona ayk\u0131r\u0131 bir durum ortaya koyacakt\u0131r!)<\/p>\n<p>Bu geli\u015ftirmeden sonra misal olarak &#8216;UsersController&#8217; isimli bir controller i\u00e7erisinde &#8216;Login&#8217; i\u015flemi i\u00e7in a\u015fa\u011f\u0131daki gibi istek kar\u015f\u0131lanabilir.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n    &#x5B;ApiController]\r\n    public class UsersController : ControllerBase\r\n    {\r\n        readonly IGoogleIdTokenValidationService _googleIdTokenValidationService;\r\n\r\n        public UsersController(IGoogleIdTokenValidationService googleIdTokenValidationService)\r\n        {\r\n            _googleIdTokenValidationService = googleIdTokenValidationService;\r\n        }\r\n\r\n        &#x5B;HttpPost]\r\n        public async Task&lt;IActionResult&gt; Login(GoogleLoginVM model)\r\n        {\r\n            Token token = await _googleIdTokenValidationService.ValidateIdTokenAsync(model);\r\n            return Ok(token);\r\n        }\r\n    }\r\n<\/pre>\n<\/div>\n<p>Backend&#8217;de ki bu geli\u015ftirmelerden sonra tek yap\u0131lmas\u0131 gereken Angular&#8217;da &#8216;SocialAuthService&#8217;in \u00e7a\u011fr\u0131ld\u0131\u011f\u0131 yerde uygun endpoint&#8217;e &#8216;SocialUser&#8217; t\u00fcr\u00fcnde veriyi post iste\u011fi e\u015fli\u011finde g\u00f6ndermektir.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { SocialAuthService, SocialUser } from '@abacritt\/angularx-social-login';\r\nimport { Component, OnInit } from '@angular\/core';\r\nimport { HttpClient } from '@angular\/common\/http'\r\n\r\n@Component({\r\n  selector: 'app-login',\r\n  templateUrl: '.\/login.component.html',\r\n  styleUrls: &#x5B;'.\/login.component.scss']\r\n})\r\nexport class LoginComponent implements OnInit {\r\n\r\n  constructor(private socialAuthService: SocialAuthService, private httpClient: HttpClient) {\r\n    this.socialAuthService.authState.subscribe((user: SocialUser) =&gt; {\r\n      httpClient.post(&quot;https:\/\/localhost:7054\/api\/users\/&quot;, user).subscribe(token =&gt; console.log(token))\r\n    });\r\n  }\r\n\r\n  ngOnInit(): void {\r\n  }\r\n}\r\n<\/pre>\n<\/div>\n<h3 style=\"color:#e83e8c;\">Test Edelim<\/h3>\n<p>Geli\u015ftirdi\u011fimiz uygulaman\u0131n hem Angular hem de API kanatlar\u0131n\u0131 aya\u011fa kald\u0131r\u0131p test edersek e\u011fer;<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2022\/06\/Asp.NET-Core-Identity-Angular-14-Esliginde-Google-Login.gif\" alt=\"Asp.NET Core Identity + Angular 14 E\u015fli\u011finde Google Login\" width=\"800\" height=\"254\" class=\"aligncenter size-full wp-image-25314\" \/><\/a>yukar\u0131daki gibi hem Google do\u011frulamas\u0131n\u0131n ger\u00e7ekle\u015ftirildi\u011fini hem de backend&#8217;den client&#8217;a yetkilendirilmi\u015f bir token \u00fcretilip g\u00f6nderildi\u011fini g\u00f6rm\u00fc\u015f olaca\u011f\u0131z.<\/p>\n<p>Nihai olarak,<br \/>\nAngular 14 ile Google hesab\u0131 \u00fczerinden d\u0131\u015f kaynak login i\u015fleminin backend&#8217;de Asp.NET Core Identity mekanizmas\u0131 e\u015fli\u011finde nas\u0131l yap\u0131laca\u011f\u0131n\u0131 incelemi\u015f ve tatbik etmi\u015f olduk. \u0130\u00e7erik s\u00fcrecinde geli\u015ftirdi\u011fimiz uygulamalar\u0131n bahsedilmeyen t\u00fcm detaylar\u0131n\u0131 a\u015fa\u011f\u0131daki github adreslerinden g\u00f6rebilir ve inceleyebilirsiniz.<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<p>AngularGoogleLoginExampleAPI : <a href=\"https:\/\/github.com\/gncyyldz\/AngularGoogleLoginExampleAPI\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/gncyyldz\/AngularGoogleLoginExampleAPI<\/a><br \/>\nAngularGoogleLoginExampleClient: <a href=\"https:\/\/github.com\/gncyyldz\/AngularGoogleLoginExampleClient\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/gncyyldz\/AngularGoogleLoginExampleClient<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde, Angular ile geli\u015ftirdi\u011fimiz uygulamalarda kullan\u0131c\u0131lar\u0131n sosyal profillerini kullanarak giri\u015f yapmak istemeleri durumlar\u0131na istinaden Google hesab\u0131 \u00fczerinden bir oturum a\u00e7ma \u00e7al\u0131\u015fmas\u0131 sergiliyor olaca\u011f\u0131z. Tabi bu \u00e7al\u0131\u015fmada, Angular k\u0131sm\u0131nda @abacritt\/angularx-social-login k\u00fct\u00fcphanesinden istifade edecek&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":25317,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4446,2420,4610,3102,300],"tags":[4611,4612,3107,4613],"class_list":["post-25280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-6","category-angular","category-angular-14","category-asp-net-core","category-google-teknolojileri","tag-angular-google-login","tag-asp-net-core-google-login","tag-google-login","tag-google-apis-auth"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/25280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=25280"}],"version-history":[{"count":27,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/25280\/revisions"}],"predecessor-version":[{"id":25320,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/25280\/revisions\/25320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/25317"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=25280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=25280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=25280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}