0

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; 
    } 
} 

答えて

0

私は、単一のエンドポイントにオープンIdを接続ミドルウェアを適用するために地図IAppBuilder拡張子を使用して、競合認証プロバイダの問題をsidestepped。このようにして、ソーシャルログインフローはミドルウェアによって完全に処理され、認証Cookieは両方の認証プロバイダによって共有されます。

public void Configuration(IAppBuilder app) 
{ 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = "Cookies", 
     LoginPath = new PathString("/Account/Login") 
    }); 
    app.Map(new PathString("/Account/SocialLogin"), ctx => 
    { 
     ctx.UseOpenIdConnectAuthentication(Options()); 
    }); 
} 

そしてAccountControllerビット:

public class AccountController : Controller 
{ 
    [HttpGet] 
    public ActionResult Login(string returnUrl) 
    { 
     return View(new LoginViewModel { ReturnUrl = returnUrl }); 
    } 

    [HttpPost] 
    public async Task<ActionResult> Login(LoginViewModel vm) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(vm); 
     } 

     // Call IdentityServer here with credentials 
     TokenResponse token = await GetToken(vm.UserName, vm.Password); 

     // Validate token and do the Owin Authentication SignIn 
     // Redirect to 'ReturnUrl' 

     await SignInAsync(token); 

     return new RedirectResult(vm.ReturnUrl); 
    } 

    [Authorize] 
    public ActionResult SocialLogin(string returnUrl) 
    { 
     return new RedirectResult(returnUrl); 
    } 

    // Some helpers omitted 
} 
関連する問題