前言
同事在練習 將 Microsoft 驗證程式庫納入 ASP.NET MVC Web 應用程式 時,想要在 AccountController 的 SignIn() 中,在轉址到 Azure AD 驗證登入前先存入某些參數,然後在 Azure AD 驗證登入成功後,取回該參數。
準備環境
透過 Visual Studio 新增 ASP.NET MVC 專案,並加入以下的 Nuget 套件,
- Microsoft.Owin
- Microsoft.Owin.Host.SystemWeb
- Microsoft.Owin.Security
- Microsoft.Owin.Security.Cookies
- Microsoft.Owin.Security.OpenIdConnect
- Microsoft.Graph
測試
要額外傳送參數,可以透過 AuthenticationProperties 的 Dictionary 來加入額外的參數。
所以在 AccountController 的 SignIn() 中,可以加入一個 tuid 的參數,如下,
1 | public void SignIn() |

Azure AD 登入驗證完成後,會進入到 OnAuthorizationCodeReceivedAsync Method 之中,
就可以透過參數 notification 中的 AuthenticationTicket.Properties.Dictionary 取出參數值,如下,
1 | private async Task OnAuthorizationCodeReceivedAsync( |

參考資源
將 Microsoft 驗證程式庫納入 ASP.NET MVC Web 應用程式
Passing state through authentication in ASP.NET Core