2017-07-11 13 views
0

Azure AD認証とフォーム認証の両方をサポートできるMVC ASP.NET C#アプリケーションを開発しようとしています。フォームとAzure AD認証

私はそれについて読み、以下の結論に達したました:

私は、フォーム認証とAzureのADログインに私をリダイレクトし、ボタンのログインフォームを持っています。

私がADにログインすると、自動的に http://localhost/login.aspx?ReturnUrl=%2fにリダイレクトされます。

Startup.cs

public partial class Startup 
    { 
     private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; 
     private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; 
     private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"]; 
     private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"]; 

     string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); 

     public void ConfigureAuth(IAppBuilder app) 
     { 
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

      app.UseOpenIdConnectAuthentication(
       new OpenIdConnectAuthenticationOptions 
       { 
        ClientId = clientId, 
        Authority = authority, 
        PostLogoutRedirectUri = postLogoutRedirectUri, 
        RedirectUri = postLogoutRedirectUri, 
        Notifications = new OpenIdConnectAuthenticationNotifications 
        { 
         AuthenticationFailed = context => 
         { 
          context.HandleResponse(); 
          context.Response.Redirect("/Error?message=" + context.Exception.Message); 
          return Task.FromResult(0); 
         } 
        } 
       }); 
     } 
    } 

AccountController.cs

public void SignIn() 
     { 
      // Send an OpenID Connect sign-in request. 
      if (!Request.IsAuthenticated) 
      { 
       HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType); 
      } 
     } 
     public void SignOut() 
     { 
      // Send an OpenID Connect sign-out request. 
      HttpContext.GetOwinContext().Authentication.SignOut(
       OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType); 
     } 

     public void EndSession() 
     { 
      // If AAD sends a single sign-out message to the app, end the user's session, but don't redirect to AAD for sign out. 
      HttpContext.GetOwinContext().Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType); 
     } 

それはアプリがMVCであることを与えて、http://localhost/login.aspx?ReturnUrl=%2fに私をリダイレクトしない理由私の質問は次のとおりです。次のコードを使用して

私のプロジェクトにはaspxがありません。

+0

あなたの質問は何を?私はポストでそれを見つけることができない;) –

+0

こんにちは@RickvandenBosch。私の問題はなぜそれが私をlocalhost/login.aspxにリダイレクトするのかということでした。 –

+0

MVCプロジェクトを作成する際にフォーム認証が**個々のユーザーアカウント**を選択することを意味するかどうかについて、問題を正しく理解するために確認したいと思いますか? –

答えて

0

デフォルトでは、フォーム認証のログインURLはLogin.aspxです。あなたは、web.configファイルでWindowsフォーム認証のログインURLを指定することができます。

<authentication mode="Forms"> 
    <forms loginUrl="/account/signin" defaultUrl="/" /> 
</authentication> 

さらに詳しい情報:FormsAuthentication.LoginUrl Property

+0

あなたの答えをありがとう。しかし私は、FormsとAzureの両方のAD認証を使用しています。 –

+0

これは、フォーム認証のログインURLを指定する必要がないことを意味しません。リダイレクトが発生しています。なぜなら、リダイレクトが存在しない間に.aspxにリダイレクトされた理由を尋ねました。答え:フォーム認証のデフォルトのログインURLです。 Azure認証が完了したことを( '' 'User.IsAuthenticated?' '')確認し、そこからリダイレクトするためには、AccountControllerをスマートにする必要があります。 –

+0

私は既にweb.configにそれを持っていました。問題はSignInから、RedirectUrlは内部に文字列がありませんでした。 Azureでログインした後、SignedOn()を作成した新しいメソッドにリダイレクトされますが、IsAuthenticatedがまだfalseであるため、ユーザーがAzure ADで認証されているかどうかを確認する方法がわかりません。 –

関連する問題