2016-08-01 15 views
1

我試圖使用Azure AD B2C將身份驗證添加到Web表單應用程序。不幸的是,除了this web forms tutorial之外,我發現每個教程都是針對MVC的。使用該教程,我已將此代碼添加到我的startup.auth.cs中:使用B2C的Web表單身份驗證

public partial class Startup { 

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301883 
    public void ConfigureAuth(IAppBuilder app) 
    { 
     app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

     app.UseOpenIdConnectAuthentication(
      new OpenIdConnectAuthenticationOptions 
      { 
       ClientId = "my-client-id", 
       Authority = "https://login.microsoftonline.com/my-tenant" 
      }); 
    } 
} 

而且工作正常。然而,我需要註冊功能以及登錄,但我無法弄清楚如何去做,因爲我發現的所有內容都是針對MVC的,我不確定如何將其轉換爲我需要的。我已經嘗試添加如下代碼:

app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(_SignUpPolicyId)); 
app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(_ProfilePolicyId)); 
app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(_SignInPolicyId)); 

以及創建登錄頁面上的三個按鈕,但點擊它們只是給出了一個404錯誤,並沒有額外的信息,所以我不知道如何做這項工作,或者即使我朝着正確的方向前進。我以前從未與B2C合作過,所以如果任何人有任何建議/已經爲Web表單做過這樣的事情,我會非常感激一些提示或示例代碼。

回答

1

您使用的是使用「本地帳戶」 enter image description here

本地帳戶指的是本地數據庫,併爲每個Idenity提供商將添加一個按鈕的例子。

嘗試將身份驗證更改爲「無身份驗證」(並自行添加所有文件)或「工作和學校帳戶」(連接到AD,將其轉換爲B2C)。

你會看到一個重定向到https://login.microsoftonline.com/yourtenant.onmicrosoft.com/ ....

接下來的步驟都遵循相同的步驟與MVC例如實現相同的代碼塊。

確保到的NuGet包更新到新版本(1.0和4.0是默認):

<package 
    id="Microsoft.IdentityModel.Protocol.Extensions" 
    version="1.0.2.206221351" 
    targetFramework="net46" /> 
<package 
    id="System.IdentityModel.Tokens.Jwt" 
    version="4.0.2.206221351" 
    targetFramework="net46" /> 

,代碼:

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

     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

     app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(signInPolicyId)); 
    } 

private OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(string policy) 
    { 

     return new OpenIdConnectAuthenticationOptions 
     { 
      MetadataAddress = string.Format(aadInstance, tenant, policy), 
      AuthenticationType = policy, 

      ClientId = clientId, 
      RedirectUri = "https://localhost:44300/", 
      PostLogoutRedirectUri = redirectUri, 
      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
      }, 

      Scope = "openid", 
      ResponseType = "id_token", 

      TokenValidationParameters = new TokenValidationParameters 
      { 
       NameClaimType = "name", 
      }, 
     }; 
    } 

添加/Account/SignIn.aspx頁面,並在後面的代碼中放置從MVC示例SignIn的代碼:

if (!Request.IsAuthenticated) 
     {     
      // To execute a policy, you simply need to trigger an OWIN challenge. 
      // You can indicate which policy to use by adding it to the AuthenticationProperties using the 
      // PolicyKey provided. 
      HttpContext.Current.GetOwinContext().Authentication.Challenge(
       new AuthenticationProperties() 
       { 
        RedirectUri = "/", 
       }, 
       appConfiguration.B2CSignInPolicyId); 
     } 
+0

我最終將項目切換到MVC,因爲al我們其餘的網站都是用MVC完成的,所以我們決定這個應該也是。所以,我沒有真正檢查你的答案,因爲它不再相關,但我感謝你花時間,所以我會接受,以便問題得到解決。謝謝! – issharp

+0

我很欣賞評論 – Danrex