How to requirect to specific page in blazor application in Microsoft B2C Authentication?

dev3
dev3
154 Points
3 Posts

I'm trying to redirect back to specific page in my blazor application. I'm using Azure B2C authentication in .net 8 blazor application. It always return to route of my application i.e. /.

It's generating logic url as:

https://mydomain.b2clogin.com/mydomain.onmicrosoft.com/b2c_1_susi/oauth2/v2.0/authorize?client_id=&redirect_uri=https%3A%2F%2Flocalhost%3A44312%2Fsignin-oidc&response_type=id_token&scope=openid%20profile

It's redirecting to signin-oidc.

Views: 332
Total Answered: 1
Total Marked As Answer: 1
Posted On: 26-May-2024 22:25

Share:   fb twitter linkedin
Answers
Smith
Smith
2890 Points
78 Posts
         

/signin-oidc   is the default  OpenID Connect (OIDC). To redirect to the specific page in blazor server app do following:

  1. Create a componet page say: LoginRedirect.razor
    @namespace AppBlazor.Shared

    @using System.Web
    @attribute [AllowAnonymous]
    @inject NavigationManager _navigationManager

    @code {
        protected override void OnAfterRender(bool firstRender)
        {
            string returnUri = _navigationManager
            .ToBaseRelativePath(_navigationManager.Uri);

            string redirectUri =
                string.IsNullOrWhiteSpace(returnUri) ?
                    string.Empty : $"?redirectUri=/{returnUri}";

            _navigationManager.NavigateTo(
                uri: $"MicrosoftIdentity/Account/SignIn{redirectUri}",
                forceLoad: true);
        }
    }
  2. Modify App.razor
    @namespace AppBlazor

    <CascadingAuthenticationState>
        <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
            <Found Context="routeData">
                <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                    <NotAuthorized>
                        <LoginRedirect />
                    </NotAuthorized>
                </AuthorizeRouteView>
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </CascadingAuthenticationState>
Posted On: 30-May-2024 03:17
Great!
 - Brian  30-May-2024 03:42
Thanks
 - dev3  26-Jul-2024 02:46
 Log In to Chat