ASP.NETには、ajax呼び出し用の単一ページアプリケーションとWeb APIを提供するMVCを使用するWebアプリケーションがあります。ASP.NET OWINのトークンベースの認証でCookieを置き換えるOpenIdConnectコードの承認フロー
認証では、認証局のためのAzure ADでMicrosoft.OwinとOpenIdConnectを使用しています。 OAUTHフローはサーバーサイドコード認可です。我々はそのルートエンドその後、次のMVCコントローラ
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);
}
の方法にマップのURLにナビゲートサインインの はその後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*/
},
});
}
をクリックしたときユーザーがアプリケーションに接続すると、ASP.NET Cookieを使用して、クライアントアプリケーションとASP.netサーバー間で認証されます。 の代わりにトークンベースのアプローチを使用したいと考えています。興味があればthis is the reason。
私はMicrosoft.Owin.Security.OAuthによってNugetパッケージにMicrosoft.Owin.Security.Cookiesを を交換しようとしたとStartup.cs でapp.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
によって
app.UseCookieAuthentication(cookieAuthenticationOptions);
を交換し、私のAccountControllerのチャレンジをHttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
からHttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, OAuthDefaults.AuthenticationType);
問題は、Cookieを使用すると、セットクッキーが、指定されたURLにリダイレクトされている間にフローが完了したときに、Webリクエストで自動的に送信されました。私はUseOAuthBearerAuthenticationでOWINによって生成されたベアラを見つけることができます (もしあれば)**、**どこで、私は私のクライアントのSPAに戻っ
注それを送信する必要がある場合:のオープンソースのサンプルを私たちがやろうとしていることはthis github repositoryにあります。
ヴィットリオも#2であなたを助ける良い記事を持っています:http://www.cloudidentity.com/blog/2014/04/28/use-owin-azure-ad-to-secure-both-mvc -ux-and-web-api-in-same-project/ – dstrockis
thx!私が間違っていない限り。 Opt1、100%暗黙のフローは適応されません。サードパーティのapiリクエスト(オフィス365グラフなど)に私たちのロジックで「参加する」ためのサーバーロジックがたくさんあり、デーモンもあります。 OnBehalfOfは私たちが必要としているようですが、まだAzure ADv2.0ではサポートされておらず、現在v2.0に移行しようとしています... Opt2では、あなたは私と同じJWTを再利用するように提案していますAzureADと私のサーバーは私のクライアントと私のサーバーの間で権限を与えるのですか?実際に私は、図書館が単純な古いクッキーではなく、ASP.NETでトークンを発行できるようにすることを望んでいました。 –
ああ、私は参照してください。はいいいえOBO v2のために、申し訳ありません。あなたは#2のあなたの記述で正しいです。また、AADトークンを使用してクライアントとサーバーとの対話を許可する代わりに、AADトークンを使用して独自のトークンタイプを初期化し、代わりに使用することもできます。 JWTを生成するのに役立ついくつかのオープンソースライブラリがあります。そのうちの1つを使用できます。 #2は、それに付属する暗号とキー管理を含めて、あなたがそれをやることからあなたを救うだけです。 – dstrockis