Adding Social Login to your ASP.NET core 2.1

Trong bài viết trước, ta đã tạo Login and Registration with Identity in ASP CORE 2.1, thì trong nội dung chia sẻ bài dưới đây ta sẽ tích hợp đăng nhập mạng xã hội vào project của ta
Đầu tiên ta cần chuẩn bị như sau:

Bước 1: Login google của bạn
Bước 2: Vào https://console.developers.google.com/?hl=vi 
Bước 3: Chọn mục Credentials-> Create Credentials->OAuth 2.0 Client IDs
Bước 4: Chọn Web Application và cài đặt nó thôi
Bước 5: Sau khi hoàn thành bạn sẽ được khóa Client ID & Client Secret , hay copy nó để tí ta sử dụng
 

# Google external login setup in ASP.NET Core

Adding Social Login to your ASP.NET core 2.1

Adding Social Login to your ASP.NET core 2.1

Adding Social Login to your ASP.NET core 2.1

Adding Social Login to your ASP.NET core 2.1

Tiếp tục để add Client ID & Client Secret vào project, ta sử dụng như sau: Tools->Nutget Package Manager->Package Manager Console , sau đó chạy câu lệnh sau

dotnet user-secrets set "Authentication:Google:ClientId" "<client-id>"
dotnet user-secrets set "Authentication:Google:ClientSecret" "<client-secret>"

Adding Social Login to your ASP.NET core 2.1

Hãy đặt các khóa Client ID & Client Secret mà ta vừa tạo hồi nảy vào, sao khi chạy 2 câu lệnh bên trên xong, để biết được Client ID & Client Secret nằm ở đâu, ta hãy bầm Clict Right project-> Manager User Secret bạn sẽ thấy các key vừa thêm vào
Ok, vậy là xong phần thiết lập key, giờ hãy mở Startup.cs trong project lên và thêm câu lệnh sau

services.AddAuthentication()
  .AddGoogle(options =>
    {
       IConfigurationSection googleAuthNSection =
            Configuration.GetSection("Authentication:Google");

           options.ClientId = googleAuthNSection["ClientId"];
           options.ClientSecret = googleAuthNSection["ClientSecret"];
    });

Okay, giờ ta Run project và đăng nhập bằng google xem được không!

# Facebook external login setup in ASP.NET Core

Đầu tiền bạn vào https://developers.facebook.com/ để tạo một ứng dụng, sau đó bạn cấu hình như hình dưới đây

Adding Social Login to your ASP.NET core 2.1

Adding Social Login to your ASP.NET core 2.1
Sau khi xong bạn sẽ được AppID & Secret, bạn tiến hành cấu hình nó trong project của bạn

dotnet user-secrets set "Authentication:Facebook:AppId" "<app-id>"
dotnet user-secrets set "Authentication:Facebook:AppSecret" "<app-secret>"

Tiếp theo hãy mở Startup.cs lên và chỉnh sửa như sau:

services.AddAuthentication()
               .AddGoogle(options =>
               {
                   IConfigurationSection googleAuthNSection =
                       Configuration.GetSection("Authentication:Google");

                   options.ClientId = googleAuthNSection["ClientId"];
                   options.ClientSecret = googleAuthNSection["ClientSecret"];
               })
            .AddFacebook(facebookOptions =>
            {
                facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
                facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
            });

Sau đó bạn Run project lên và xem có chức năng login bằng facebook chưa

Adding Social Login to your ASP.NET core 2.1

Bài Viết Liên Quan

Messsage

Ủng hộ tôi bằng cách click vào quảng cáo. Để tôi có kinh phí tiếp tục phát triển Website!(Support me by clicking on the ad. Let me have the money to continue developing the Website!)