C# 基于B2C的Web表单认证
我正在尝试使用Azure AD B2C将身份验证添加到web表单应用程序中。不幸的是,我找到的每个教程都是针对MVC的,除了。使用该教程,我将以下代码添加到startup.auth.cs中:C# 基于B2C的Web表单认证,c#,asp.net,webforms,azure-ad-b2c,C#,Asp.net,Webforms,Azure Ad B2c,我正在尝试使用Azure AD B2C将身份验证添加到web表单应用程序中。不幸的是,我找到的每个教程都是针对MVC的,除了。使用该教程,我将以下代码添加到startup.auth.cs中: public partial class Startup { // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301883
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表单有任何建议/做过类似的事情,我非常感谢您提供一些提示或示例代码。您使用的示例是使用“本地帐户” 本地帐户意味着一个本地数据库,对于每个Idenity提供者,它将添加一个按钮 尝试将身份验证更改为“无身份验证”(并自己添加所有文件)或“工作和学校帐户”(连接到广告,因此将其转换为B2C) 您将看到重定向到 接下来的步骤将遵循与MVC示例相同的步骤,实现相同的代码片段 确保将nuget软件包更新到较新版本(默认为1.0和4.0): 添加/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);
}
我最终将项目改为MVC,因为我们所有其他的站点都是用MVC完成的,所以我们决定这一个也应该是。所以,我实际上没有检查你的答案,因为它不再相关,但我感谢你花时间,所以我无论如何都会接受,这样问题就解决了。谢谢我感谢你的评论
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",
},
};
}
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);
}