IdentityServer3を使用して、asp mvc Webサイトでユーザー認証を処理しています。IdentityServer3:ソーシャルオプションと手動資格情報ログインを組み合わせる
ログイン画面は、アプリケーション自体のビューでホストされます(Identity Serverの暗黙的フローを使用しない)。また、ログイン画面のボタンをクリックしてユーザーが選択する「Googleでログイン」オプションも提供しています。
[オーソライズ]は任意のページは、ログイン画面に認証されていないユーザーをリダイレクトすべきである:
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
LoginPath = new PathString("/Account/Login")
});
}
アカウントコントローラ概要を以下に示します。 「フォーム認証」シナリオが機能します(as described here)。
「Googleでログインする」ボタンは、「acr_values」パラメータを指定してas described here(IDサーバーのログインページをスキップし、Googleに直接行く)で機能します。
私はGoogleにログインした後、OpenID Connectからのコールバックをどのように処理する必要がありますか? OpenId Connect middlewareを追加しようとしましたが、Cookie認証で 'LoginPath'機能がうまく動作しません。認証されていないユーザーは、ローカルログイン画面ではなくIDサーバーのログイン画面にリダイレクトされるようになりました。
このシナリオに対応するIdentityServer samplesには何も表示されません。
public class AccountController : Controller
{
[HttpGet]
public ActionResult Login()
{
ViewBag.GoogleLogin = CreateLoginUrl("Google");
return View();
}
[HttpPost]
public ActionResult Login(LoginViewModel vm)
{
// Call IdentityServer here with credentials
// Validate token and do the Owin Authentication SignIn
// Redirect to 'ReturnUrl'
// If errors:
return View(vm);
}
public ActionResult Callback()
{
// What goes here??
return new RedirectResult("/");
}
private string CreateLoginUrl(string provider)
{
var state = Guid.NewGuid().ToString("N");
var nonce = Guid.NewGuid().ToString("N");
var request = new AuthorizeRequest(new Uri("https://localhost:44312/connect/authorize"));
var startUrl = request.CreateAuthorizeUrl(
clientId: "mvc",
responseType: "id_token token",
scope: "openid profile roles sampleApi",
redirectUri: "https://localhost:44319/Account/Callback",
state: state,
acrValues: "idp:" + provider,
nonce: nonce);
return startUrl;
}
}