Home > front end >  How to set up a Sign In only Azure B2C user flow policy? Getting HTTP401 error
How to set up a Sign In only Azure B2C user flow policy? Getting HTTP401 error

Time:10-11

In my AccountController I have the following methods:

    /*
     *  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;
    }

The UserFlow is set up inside of Azure, called B2C_1_signup, and that's what Globals.SignUpPolicyId evaluates to. Yet, whenever I test it out, I get an HTTP 401 error.

Here's the razor code that creates my button/link:

 @Html.ActionLink("Sign Up!", "SignUp", "Account", routeValues: null, htmlAttributes: new { id = "signUpLink", @class = "btn btn-default" })

Whenever I test the link provided by Microsoft inside of the B2C Tenant, it brings up the Sign Up page correctly.

Here's the cleansed link provided by Microsoft for testing:

 https://mytenantname.b2clogin.com/mytenantname.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_signup&client_id=RANDOM_GUID&nonce=defaultNonce&redirect_uri=http://localhost:1111&scope=openid&response_type=id_token&prompt=login

What am I missing??

CodePudding user response:

• The redirect URI string defined in the account controller should be defined in the app config settings as a private static string and the B2C policies as different identifiers as public static strings due to which when during the user flow, authentication redirection will happen through by referencing the concerned app config string rather than finding it in the controller file itself. Since, you are encountering HTTP 401 error due to authentication issues related to the browser session.

Please find below the app controller sample methods calling the Azure AD B2C policies which works correctly as defined below for sign up, sign in and profile of the user to be authenticated: -

 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());
        }
    }
}

Also, refer the below link for more clarified information: -

Azure AD B2C User flow output

  • Related