Bee.OAuth2.AspNet
2.0.0
dotnet add package Bee.OAuth2.AspNet --version 2.0.0
NuGet\Install-Package Bee.OAuth2.AspNet -Version 2.0.0
<PackageReference Include="Bee.OAuth2.AspNet" Version="2.0.0" />
<PackageVersion Include="Bee.OAuth2.AspNet" Version="2.0.0" />
<PackageReference Include="Bee.OAuth2.AspNet" />
paket add Bee.OAuth2.AspNet --version 2.0.0
#r "nuget: Bee.OAuth2.AspNet, 2.0.0"
#:package Bee.OAuth2.AspNet@2.0.0
#addin nuget:?package=Bee.OAuth2.AspNet&version=2.0.0
#tool nuget:?package=Bee.OAuth2.AspNet&version=2.0.0
Bee.OAuth2.AspNet
Bee.OAuth2.AspNet is an OAuth2 authentication library designed for ASP.NET WebForms / MVC, supporting Google, Facebook, LINE, Azure, and Auth0.
📦 Installation
Install via NuGet Package Manager:
dotnet add package Bee.OAuth2.AspNet
🌍 Supported OAuth2 Providers
- ✅ LINE
- ✅ Azure (Microsoft Entra ID)
- ✅ Auth0
🚀 Usage Example (ASP.NET WebForms)
Register Google OAuth2 in Global.asax
using Bee.OAuth2;
using Bee.OAuth2.AspNet;
protected void Application_Start()
{
var options = new GoogleOAuth2Options()
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
RedirectUri = "your-redirect-uri",
UsePKCE = true
};
var client = new OAuth2Client(options);
OAuth2Manager.RegisterClient("Google", client);
}
Redirect to OAuth Authorization Page in Login Page
OAuth2Manager.RedirectToAuthorization("Google");
Validate OAuth2 Callback and Retrieve User Information
var result = await OAuth2Manager.ValidateAuthorization();
Response.Write(
$"ProviderName : {result.ProviderName}<br/>" +
$"UserID : {result.UserInfo.UserId}<br/>" +
$"UserName : {result.UserInfo.UserName}<br/>" +
$"Email : {result.UserInfo.Email}<br/>" +
$"RawJson : {result.UserInfo.RawJson}");
🚀 Usage Example (ASP.NET MVC)
Register Google OAuth2 in Startup.cs
using Bee.OAuth2;
using Bee.OAuth2.AspNet;
public void Configuration(IAppBuilder app)
{
var options = new GoogleOAuth2Options()
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
RedirectUri = "your-redirect-uri",
UsePKCE = true
};
var client = new OAuth2Client(options);
OAuth2Manager.RegisterClient("Google", client);
}
Redirect to OAuth2 Authorization Page in Controller
public ActionResult Login()
{
return Redirect(OAuth2Manager.GetAuthorizationUrl("Google"));
}
Validate OAuth2 Callback and Retrieve User Information in Controller
public async Task<ActionResult> Callback()
{
var result = await OAuth2Manager.ValidateAuthorization();
return Content($"ProviderName: {result.ProviderName}\n" +
$"UserID: {result.UserInfo.UserId}\n" +
$"UserName: {result.UserInfo.UserName}\n" +
$"Email: {result.UserInfo.Email}\n" +
$"RawJson: {result.UserInfo.RawJson}");
}
🔐 Key Setup
Generate a secure key for state
encryption
Bee.OAuth2.AspNet uses AES-CBC + HMAC
to protect the OAuth2 state
parameter. You must generate a 64-byte combined key and store it in the environment variable OAUTH2_STATE_KEY
.
Note:
If the environment variableOAUTH2_STATE_KEY
is not set, the state value will not be encrypted.
Instead, the client name will be encoded using Base64 only.
This provides basic obfuscation but does not guarantee confidentiality or integrity.
🔧 How to generate the key
// Use this once to generate a base64 key
var key = Bee.Base.AesCbcHmacKeyGenerator.GenerateCombinedKey();
Console.WriteLine(Convert.ToBase64String(key));
⚙️ Set the environment variable
On Windows:
- Open System Properties → Environment Variables
- Add a new User or System variable:
Variable name | Value (example) |
---|---|
OAUTH2_STATE_KEY |
VGhpcy1pcy1hLXRlc3Qta2V5LXdpdGgtNjQ... |
- Restart Visual Studio and the application.
Alternatively, you can set it using PowerShell:
[System.Environment]::SetEnvironmentVariable("OAUTH2_STATE_KEY", "your-base64-key", "User")
📜 License
This project is licensed under the MIT License.
Bee.OAuth2.AspNet(中文)
Bee.OAuth2.AspNet 是一個專為 ASP.NET WebForms / MVC 設計的 OAuth2 認證函式庫,支援 Google、Facebook、LINE、Azure、Auth0 等 OAuth2 提供者。
📦 安裝方式
透過 NuGet 安裝:
dotnet add package Bee.OAuth2.AspNet
🌍 支援的 OAuth2 提供者
- ✅ LINE
- ✅ Azure(Microsoft Entra ID)
- ✅ Auth0
🚀 使用範例(ASP.NET WebForms)
在 Global.asax 註冊 Google OAuth2
using Bee.OAuth2;
using Bee.OAuth2.Providers;
protected void Application_Start()
{
var options = new GoogleOAuth2Options()
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
RedirectUri = "your-redirect-uri",
UsePKCE = true
};
var client = new OAuth2Client(options);
OAuth2Manager.RegisterClient("Google", client);
}
在 login 頁面轉向 OAuth2 授權頁面
OAuth2Manager.RedirectToAuthorization("Google");
在 callback 頁面驗證 OAuth2 回傳授權碼,並取得用戶資料
var result = await OAuth2Manager.ValidateAuthorization();
Response.Write(
$"ProviderName : {result.ProviderName}<br/>" +
$"UserID : {result.UserInfo.UserId}<br/>" +
$"UserName : {result.UserInfo.UserName}<br/>" +
$"Email : {result.UserInfo.Email}<br/>" +
$"RawJson : {result.UserInfo.RawJson}");
🚀 使用範例(ASP.NET MVC)
在 Startup.cs 註冊 Google OAuth2
using Bee.OAuth2;
using Bee.OAuth2.Providers;
public void Configuration(IAppBuilder app)
{
var options = new GoogleOAuth2Options()
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
RedirectUri = "your-redirect-uri",
UsePKCE = true
};
var client = new OAuth2Client(options);
OAuth2Manager.RegisterClient("Google", client);
}
在 Controller 中轉向 OAuth 授權頁面
public ActionResult Login()
{
return Redirect(OAuth2Manager.GetAuthorizationUrl("Google"));
}
在 Controller 中驗證 OAuth2 回傳授權碼,並取得用戶資料
public async Task<ActionResult> Callback()
{
var result = await OAuth2Manager.ValidateAuthorization();
return Content($"ProviderName: {result.ProviderName}\n" +
$"UserID: {result.UserInfo.UserId}\n" +
$"UserName: {result.UserInfo.UserName}\n" +
$"Email: {result.UserInfo.Email}\n" +
$"RawJson: {result.UserInfo.RawJson}");
}
🔐 金鑰設定
產生用於加密 state
的安全金鑰
Bee.OAuth2.AspNet 使用 AES-CBC + HMAC
演算法保護 OAuth2 的 state
參數。你必須先產生一組 64 位元組的組合金鑰,並設定為 OAUTH2_STATE_KEY
環境變數。
注意:
如果未設定OAUTH2_STATE_KEY
環境變數,state 值將不會加密,
而是僅以 Base64 編碼 client name。
這僅提供基本遮蔽,不保證機密性或完整性。
🔧 如何產生金鑰
// 執行一次即可產生 Base64 格式的金鑰
var key = Bee.Base.AesCbcHmacKeyGenerator.GenerateCombinedKey();
Console.WriteLine(Convert.ToBase64String(key));
⚙️ 設定環境變數(Windows)
- 開啟「系統內容」 →「環境變數」
- 在「使用者變數」或「系統變數」中新增一筆:
變數名稱 | 值(範例) |
---|---|
OAUTH2_STATE_KEY |
VGhpcy1pcy1hLXRlc3Qta2V5LXdpdGgtNjQ... |
- 重啟 Visual Studio 與網站應用程式。
也可以使用 PowerShell 設定:
[System.Environment]::SetEnvironmentVariable("OAUTH2_STATE_KEY", "你的 base64 金鑰", "User")
📜 授權
本專案採用 MIT License。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net48 is compatible. net481 was computed. |
-
.NETFramework 4.8
- Bee.OAuth2 (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.