MVC を使用してシングル ページ アプリケーションを提供し、Web API を ajax 呼び出しに使用する ASP.NET で記述された Web アプリケーションがあります。
認証では、Microsoft.OwinとOpenIdConnectを Azure AD の機関として使用します。OAUTH フローは、サーバー側のコード承認です。次に、Startup.Auth.csに
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
var cookieAuthenticationOptions = new CookieAuthenticationOptions()
{
CookieName = CookieName,
ExpireTimeSpan = TimeSpan.FromDays(30),
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
SlidingExpiration = true,
};
app.UseCookieAuthentication(cookieAuthenticationOptions);
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
AuthorizationCodeReceived = (context) =>
{
/*exchange authorization code for a token
stored on database to access API registered on AzureAD (using ADAL.NET) */
},
RedirectToIdentityProvider = (RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) =>
{
/* Set the redirects URI here*/
},
});
}
サインインをクリックすると、ルートが次の MVC コントローラーのメソッドにマップされる URL に移動します。
public class AccountController : Controller
{
public void SignIn(string signalrRef)
{
var authenticationProperties = /* Proper auth properties, redirect etc.*/
HttpContext.GetOwinContext()
.Authentication.Challenge(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
public void SignOut(string signalrRef)
{
var authenticationProperties = /* Proper auth properties, redirect etc.*/
HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
次に、アプリケーションに接続されたエンド ユーザーは、ASP.NET Cookie を使用して、クライアント アプリと ASP.net サーバーの間で認証されます。代わりにトークンベースのアプローチを使用したいと考えています。興味があるなら、これが理由です。
Nuget パッケージMicrosoft.Owin.Security.Cookies をMicrosoft.Owin.Security.OAuthに置き換え、Startup.cs で置き換えようとしました
app.UseCookieAuthentication(cookieAuthenticationOptions);
にapp.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
私のAccountController ではHttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
、チャレンジを からに変更しましたHttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType, OAuthDefaults.AuthenticationType);
問題は、Cookie を使用すると、指定した URL へのリダイレクト中にフローが完了すると、Web 要求応答で set-cookieが自動的に送信されることです。UseOAuthBearerAuthentication を使用して OWIN によって生成されたベアラーはどこにありますか (存在する場合) **、**どこで、いつクライアント SPA に返送する必要がありますか?
注:私たちがやろうとしていることのオープン ソース サンプルは、この github リポジトリにあります。