如何设置仅登录 Azure B2C 用户流策略?收到 HTTP401 错误
How to set up a Sign In only Azure B2C user flow policy? Getting HTTP401 error
在我的 AccountController 中,我有以下方法:
/*
* Called when requesting to sign up or sign in
*/
public void SignUpSignIn(string redirectUrl)
{
redirectUrl = redirectUrl ?? "/";
// Use the default policy to process the sign up / sign in flow
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = redirectUrl });
return;
}
/*
* Called when requesting to sign up
*/
public void SignUp()
{
// Use the default policy to process the sign up flow
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, Globals.SignUpPolicyId);
return;
}
UserFlow 在 Azure 内部设置,称为 B2C_1_signup,这就是 Globals.SignUpPolicyId 评估的内容到。然而,每当我对其进行测试时,我都会收到 HTTP 401 错误。
这是创建我的 button/link 的剃刀代码:
@Html.ActionLink("Sign Up!", "SignUp", "Account", routeValues: null, htmlAttributes: new { id = "signUpLink", @class = "btn btn-default" })
每当我在 B2C 租户中测试 Microsoft 提供的 link 时,它都会正确显示注册页面。
这是 Microsoft 提供的用于测试的已清理 link:
https://mytenantname.b2clogin.com/mytenantname.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_signup&client_id=RANDOM_GUID&nonce=defaultNonce&redirect_uri=http%3A%2F%2Flocalhost%3A1111&scope=openid&response_type=id_token&prompt=login
我错过了什么??
• 帐户控制器中定义的重定向 URI 字符串应在应用程序配置设置中定义为私有静态字符串,B2C 策略应定义为不同的标识符,如 public 静态字符串,因此在用户使用期间流,身份验证重定向将通过引用相关的应用程序配置字符串而不是在控制器文件本身中找到它来实现。由于与浏览器会话相关的身份验证问题,您遇到了 HTTP 401 错误。
请在下面找到调用 Azure AD B2C 策略的应用程序控制器示例方法,这些方法可以按照下面的定义正确工作,用于注册、登录和个人资料
要认证的用户:-
public class AccountController : Controller
{
public void SignIn()
{
if (!Request.IsAuthenticated)
{
// To execute a policy, you simply need to trigger an OWIN challenge.
// You can indicate which policy to use by specifying the policy id as the AuthenticationType
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignInPolicyId);
}
}
public void SignUp()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignUpPolicyId);
}
}
public void Profile()
{
if (Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.ProfilePolicyId);
}
}
public void SignOut()
{
// To sign out the user, you should issue an OpenIDConnect sign out request
if (Request.IsAuthenticated)
{
IEnumerable<AuthenticationDescription> authTypes = HttpContext.GetOwinContext().Authentication.GetAuthenticationTypes();
HttpContext.GetOwinContext().Authentication.SignOut(authTypes.Select(t => t.AuthenticationType).ToArray());
}
}
}
此外,请参阅下面的 link 以获得更清晰的信息:-
https://bitoftech.net/2016/08/31/integrate-azure-ad-b2c-asp-net-mvc-web-app/
此外,找到下面的 gif 输出以供参考:-
在我的 AccountController 中,我有以下方法:
/*
* Called when requesting to sign up or sign in
*/
public void SignUpSignIn(string redirectUrl)
{
redirectUrl = redirectUrl ?? "/";
// Use the default policy to process the sign up / sign in flow
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = redirectUrl });
return;
}
/*
* Called when requesting to sign up
*/
public void SignUp()
{
// Use the default policy to process the sign up flow
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, Globals.SignUpPolicyId);
return;
}
UserFlow 在 Azure 内部设置,称为 B2C_1_signup,这就是 Globals.SignUpPolicyId 评估的内容到。然而,每当我对其进行测试时,我都会收到 HTTP 401 错误。
这是创建我的 button/link 的剃刀代码:
@Html.ActionLink("Sign Up!", "SignUp", "Account", routeValues: null, htmlAttributes: new { id = "signUpLink", @class = "btn btn-default" })
每当我在 B2C 租户中测试 Microsoft 提供的 link 时,它都会正确显示注册页面。
这是 Microsoft 提供的用于测试的已清理 link:
https://mytenantname.b2clogin.com/mytenantname.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_signup&client_id=RANDOM_GUID&nonce=defaultNonce&redirect_uri=http%3A%2F%2Flocalhost%3A1111&scope=openid&response_type=id_token&prompt=login
我错过了什么??
• 帐户控制器中定义的重定向 URI 字符串应在应用程序配置设置中定义为私有静态字符串,B2C 策略应定义为不同的标识符,如 public 静态字符串,因此在用户使用期间流,身份验证重定向将通过引用相关的应用程序配置字符串而不是在控制器文件本身中找到它来实现。由于与浏览器会话相关的身份验证问题,您遇到了 HTTP 401 错误。
请在下面找到调用 Azure AD B2C 策略的应用程序控制器示例方法,这些方法可以按照下面的定义正确工作,用于注册、登录和个人资料 要认证的用户:-
public class AccountController : Controller
{
public void SignIn()
{
if (!Request.IsAuthenticated)
{
// To execute a policy, you simply need to trigger an OWIN challenge.
// You can indicate which policy to use by specifying the policy id as the AuthenticationType
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignInPolicyId);
}
}
public void SignUp()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignUpPolicyId);
}
}
public void Profile()
{
if (Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.ProfilePolicyId);
}
}
public void SignOut()
{
// To sign out the user, you should issue an OpenIDConnect sign out request
if (Request.IsAuthenticated)
{
IEnumerable<AuthenticationDescription> authTypes = HttpContext.GetOwinContext().Authentication.GetAuthenticationTypes();
HttpContext.GetOwinContext().Authentication.SignOut(authTypes.Select(t => t.AuthenticationType).ToArray());
}
}
}
此外,请参阅下面的 link 以获得更清晰的信息:-
https://bitoftech.net/2016/08/31/integrate-azure-ad-b2c-asp-net-mvc-web-app/
此外,找到下面的 gif 输出以供参考:-