jackomo
jackomo

Reputation: 375

Azure AD B2C - ASP.NET Core SignUp Link

I have an ASP.NET Core Web App and a Azure AD B2C tenant.

In the view I can make button entries in the following style and everything is working:

<ul class="nav navbar-nav navbar-right">
     <li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="SignIn">Sign in</a></li>
     <li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="EditProfile">Edit Profile</a></li>
     <li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="ResetPassword">Reset Password</a></li>
     <li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="SignOut">Sign out</a></li>
</ul>

And in the web app appsettings.json this is configured:

"AzureAdB2C": {
    "Instance": "https://login.microsoftonline.com/tfp/",
    "ClientId": "[...]",
    "CallbackPath": "/signin-oidc",
    "Domain": "[...]",
    "SignUpSignInPolicyId": "B2C_1_SignUpOrSignInPolicy",
    "ResetPasswordPolicyId": "B2C_1_PasswordResetPolicy",
    "EditProfilePolicyId": "B2C_1_ProfileEditPolicy"
  }

I want to add a button entry for direct access to the sign up page of the SignUpOrSignInPolicy but this is not working:

<li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="SignUp">Sign up</a></li>

What do I have to change? Or is there any documentation about this "AzureADB2C Account" controller?

Upvotes: 1

Views: 1465

Answers (1)

SaurabhSharma
SaurabhSharma

Reputation: 936

You can create a custom Account Controller to present user with Sign-up link on the login page and to route to respective action. Check below code for reference -

[Route("[controller]/[action]")]
public class AccountController : Controller
{
    public IActionResult SignUp()
    {
        return this.Challenge(
            new AuthenticationProperties { RedirectUri = "/" }, "B2C_1_B2C_POC_SignUp");
    }
}

Also, modify the StartUp.cs Services by adding the authentication service for Signup Policy like below :-

services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
                .AddAzureADB2C(options => Configuration.Bind("AzureADB2C", options));
            services.AddAuthentication(options => options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme).AddOpenIdConnect("B2C_1_B2C_POC_SignUp", GetOpenIdSignUpOptions("B2C_1_B2C_POC_SignUp"))
                 .AddCookie(); 

Here, is the Method for adding OpenId SignUp Options referenced in the above code -

private Action<OpenIdConnectOptions> GetOpenIdSignUpOptions(string policy)
            => options =>
            {
                options.MetadataAddress =
                   "https://login.microsoftonline.com/abc.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=" + policy;
                options.ClientId = "xxxxxxxxxx";//this.ClientId;
                options.ResponseType = OpenIdConnectResponseType.IdToken;
                options.SignedOutCallbackPath = "/signout/" + policy;
                options.CallbackPath = "/signin-oidc";
                options.SignedOutRedirectUri = "/";
            };

Upvotes: 2

Related Questions