ASP.NET Core 3.1 - Secret Manager Configuration
在 project中, 常常需要用到第三方的 login, 所以來寫這一篇文章記錄 ASP.NET Core 3.1 的 Configuration.
2.在 console打上下面那一段 code, 是為了要 initialize the user-secrets
dotnet user-secrets init
3.我這裡使用 Google Login 做示範, 所以要去 Google Developer Console 做 Register
- 點下中間藍色的按鈕 Configure a project
- 會跑出這一個視窗, 這裡可以打上你自己想要的 Project Name
- 這裡會出現 your OAuth Client, 在上面的 dropdown 選擇 Web Server, 下面的 Authorized redirect URLs 可以去你自己的 project 複製 url, (可能是 https://localhost/12234/), 這一個 port 每一個人都不一樣, 所以要去 start your project 後複製自己的 port (我的是 https://localhost:44330/), 之後 / 加上
signin-google
, 最後的結果是https://localhost:44330/signin-google
, 並且貼在下面的那一行
-之後就會有一組 Client Id 和 Client Secret, 這樣就申請成功
4.在回到 project console, 打上下面這 2 行, 要記得把 和 換成上面自己的 Client ID 和 Client Secret, 這樣就成功了
5.想知道到底有沒有存進去, 可以在 Project 的地方按下右鍵, 選擇 Manage User Secrets, 這時候就會有一個 file 叫做 secret.json 跑出來, 裡面就會有你剛剛輸入的 Client Id 和 Client Secret
6.在 NuGet Package Manager 安裝這一個 package Microsoft.AspNetCore.Authentication.Google
7.安裝完後, 回到 startup.cs register 這一個 configuration, 把這一段code 貼上去
services.AddAuthentication() .AddGoogle(options => { IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google"); options.ClientId = googleAuthNSection["ClientId"]; options.ClientSecret = googleAuthNSection["ClientSecret"]; });
留言
張貼留言