English 中文(简体)
Blazor Server App : Unable to unprotect the message.State
原标题:

I am getting an Exception as above in my Blazor server application.

Below is my Program.cs File

builder.Services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
.AddOpenIdConnect(options =>
{
    // options.SkipUnrecognizedRequests = true;
    options.ClientId = builder.Configuration["AzureAdB2C:ClientId"];
    options.ClientSecret = builder.Configuration["AzureAdB2C:ClientSecret"];
    options.ResponseType = OpenIdConnectResponseType.CodeToken;
    options.Scope.Add("openid");
    options.Scope.Add("https://test.onmicrosoft.com/SampleAD_B2C/task.read")
    options.SaveTokens = true;
    options.CallbackPath = "/signin-oidc";
    options.Authority = $"https://{builder.Configuration["AzureAdB2C:Domain"]}/tfp/{builder.Configuration["AzureAdB2C:Domain"]}/{builder.Configuration["AzureAdB2C:SignUpSignInPolicyId"]}/v2.0/";
    //options.BackchannelHttpHandler = new HttpClientHandler
    //{
    //    ServerCertificateCustomValidationCallback =
    //                           HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
    //};
})
//.AddMicrosoftIdentityWebApp(options =>
//{
//    builder.Configuration.Bind("AzureAdB2C", options);
//    options.Prompt = "login";
//    //options.Events.OnRemoteFailure = OnAuthenticationFailed; // Optional: Handle authentication failure
//})
//.EnableTokenAcquisitionToCallDownstreamApi()
////.AddMicrosoftGraph(builder.Configuration.GetSection("GraphApi"))
//.AddInMemoryTokenCaches();
//.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAdB2C"));

Then I created one LoginComponen.razor Page which has login Button that redirects to Azure AD B2C login. Below is the snippet for the same

<CascadingAuthenticationState>
<AuthorizeView>
    <Authorized>
        <h3>You are already logged in!</h3>
        <button class="btn btn-primary" @onclick="Logout">Logout</button>
    </Authorized>
    <NotAuthorized>
        <h3>Welcome to My Blazor App</h3>
        <button class="btn btn-primary" @onclick="Login">Login</button>
    </NotAuthorized>
</AuthorizeView>
@code {
private async Task Login()
{
    var k = protectionProvider.CreateProtector(Guid.NewGuid().ToString());
    string authorizationEndpoint = $"https://test.b2clogin.com/test.onmicrosoft.com/oauth2/v2.0/authorize";
    string clientId = "7e8bc5d2-d134-4951-8253-acf2c8ffa03d";
    // string scope = "openid";
    string scope = "openid https://test.onmicrosoft.com/SampleAD_B2C/task.read";
    string responseType = OpenIdConnectResponseType.CodeToken;
    //string responseType = "code";
    //  string responseMode = "query";
    //  string responseMode = "fragment"; //"form_post";// "query";
    string responseMode = OpenIdConnectResponseMode.FormPost;// "form_post";
    string state =  Guid.NewGuid().ToString();
    string nonce = Guid.NewGuid().ToString();
    string policy = "B2C_1_signup_signin";
    // string scopes = "task.read";
    string redirectUri = NavigationManager.BaseUri.TrimEnd( / ) + "/signin-oidc";
    //&redirect_uri={Uri.EscapeDataString(redirectUri)}
    string url = $"{authorizationEndpoint}?client_id={clientId}&scope={scope}&response_type={responseType}&response_mode={responseMode}&redirect_uri={Uri.EscapeDataString(redirectUri)}&State={state}&nonce={nonce}&p={policy}"; 
    NavigationManager.NavigateTo(url, forceLoad: true);
}}

After writing this one I already redirects to Azure B2C Login Page where I perform Login and When it redirects to /signin-oidc then inside the console I am getting access_token state values but I get the error which I am attachingenter image description here as a screenshot

A quick help will be appreciated.

问题回答

暂无回答




相关问题
Blazor Server App : Unable to unprotect the message.State

I am getting an Exception as above in my Blazor server application. Below is my Program.cs File builder.Services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme) .AddOpenIdConnect(...

System Text JsonSerializer Deserialization of TimeSpan

In researching how to deserialize a TimeSpan using Newtonsoft s JSON.net I came across code in my current project that did not use Json.net. It used System.Text.Json.JsonSerializer and appeared to not ...

TCP Connection Creation and Closing Event Hooking [closed]

What helper classes C# provides to monitor all TCP connection on an OS. A piece of Sample code would also be appreciated. Note: Original intent was to monitor this on Windows but would be nice to do ...

热门标签