1

我正在尝试将使用 Azure AD B2C 的身份验证添加到Web 表单应用程序。不幸的是,我发现的每个教程都是针对 MVC 的,除了这个 web forms tutorial。使用该教程,我已将此代码添加到我的 startup.auth.cs 中:

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 表单有任何建议/做过此类事情,我将非常感谢一些提示或示例代码。

4

1 回答 1

3

您正在使用的示例是使用“本地帐户” 在此处输入图像描述

本地帐户意味着一个本地数据库,并且对于每个身份提供者,它将添加一个按钮。

尝试将身份验证更改为“无身份验证”(并自己添加所有文件)或“工作和学校帐户”(连接到 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);
        }
于 2016-08-02T12:18:41.213 回答