私は、Azure ADで認証するためのASP Net Core 2.0プロジェクトをセットアップしました(OIDCを使用するVS2017の標準Azure AD Identity認証テンプレートを使用します)。すべて正常に動作しており、アプリケーションはベースURL(/)に戻り、認証が成功した後にHomeController.Indexアクションを実行します。Azure AD認証の後にASP NetコアMVCの別のコントローラアクションにリダイレクトする方法
しかし、認証後に別のコントローラアクション(AccountController.CheckSignIn)にリダイレクトして、ローカルデータベーステーブルにユーザーが既に存在するかどうかをチェックし、そうでなければ(つまり新しいユーザー)ローカルホームレコードにリダイレクトし、HomeController.Indexアクションにリダイレクトします。
HomeController.Indexアクション自体にこのチェックを入れることができますが、ユーザーがホームボタンをクリックするたびにこのチェックが実行されないようにします。ここで
はappsettings.json
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "<my-domain>.onmicrosoft.com",
"TenantId": "<my-tennant-id>",
"ClientId": "<my-client-id>",
"CallbackPath": "/signin-oidc" // I don't know where this goes but it doesn't exist anywhere in my app and authentication fails if i change it
}
で
AADの設定は、私はこの要件を処理するために、私のAccountController.CheckSignInに新しいアクションを追加...明快さを与えるのを助けるかもしれないいくつかのコードスニペットですが、I認証後にそれを呼び出す方法を見つけることができません。
public class AccountController : Controller
{
// I want to call this action after authentication is successful
// GET: /Account/CheckSignIn
[HttpGet]
public IActionResult CheckSignIn()
{
var provider = OpenIdConnectDefaults.AuthenticationScheme;
var key = User.FindFirstValue(ClaimTypes.NameIdentifier);
var info = new ExternalLoginInfo(User, provider, key, User.Identity.Name);
if (info == null)
{
return BadRequest("Something went wrong");
}
var user = new ApplicationUser { UserName = User.Identity.Name };
var result = await _userManager.CreateAsync(user);
if (result.Succeeded)
{
result = await _userManager.AddLoginAsync(user, info);
if (!result.Succeeded)
{
return BadRequest("Something else went wrong");
}
}
return RedirectToAction(nameof(HomeController.Index), "Home");
}
// This action only gets called when user clicks on Sign In link but not when user first navigates to site
// GET: /Account/SignIn
[HttpGet]
public IActionResult SignIn()
{
return Challenge(
new AuthenticationProperties { RedirectUri = "/Account/CheckSignIn" }, OpenIdConnectDefaults.AuthenticationScheme);
}
}
あなたは正しい仕事のために適切なツールを使うことを提唱しているようです。私はそれが好きです。 – JasonInVegas