I get the error System.InvalidOperationException: OnStarting cannot be set because the response has already started. when calling GetAuthorizationContextAsync(String returnUrl) which is part of Duende.IdentityServer.Services.DefaultIdentityServerInteractionService.
This method is called via OnInitializedAsync() method in a razor component which is being used as a login form/page.
I think that OnInitializedAsync() is getting called too many times. After spending some time debugging I can see that this function is called three times when navigating to the page. On the third time it throws the error and the connection is dropped. The sequence is as follows:
- First call of OnInitializedAsync - I assume a pre render, executes okay.
- Second call of OnInitializedAsync - executes okay. Browser empty but is view is rendered after this function finalizes.
- Third call, don't know why it is being called as by this point the form has already rendered. The error is thrown and the connection dropped.
Here is my code:
@page "/Account/login"
@inject IIdentityServerInteractionService interaction
@rendermode InteractiveServer
<EditForm FormName="login-form" Model="@model" OnValidSubmit="UserLogin">
<InputText Placeholder="Enter your username"
@bind-Value=model.Username/>
<InputText Placeholder="Enter your password"
@bind-Value=model.Password/>
<button type="submit">Login</button>
</EditForm>
@code {
[SupplyParameterFromQuery]
private string? returnUrl { get; set; }
[SupplyParameterFromForm]
private LoginInputModel model { get; set; } = new();
//error thrown on third execution of this method
protected override async Task OnInitializedAsync()
{
var vm = await BuildLoginViewModelAsync(returnUrl);
}
private async Task UserLogin() { }
private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl)
{
//error thrown
var context = await interaction.GetAuthorizationContextAsync(returnUrl);
return new LoginViewModel();
}
}
Also, since it is important here is a formatted version of a sample returnUrl.
"/connect/authorize/callback?
client_id=webapp&
redirect_uri=https%3A%2F%2Flocalhost%3A7188%2Fsignin-oidc&
response_type=code&
scope=openid%20profile%20core-api&
code_challenge=neweRQ7n0ozZYVRUAmkK5Sy9HA_W4zVQZtagNWc_Nx4&
code_challenge_method=S256&
response_mode=form_post&
nonce=638421522594225203.MWZkNTNiNDgtZjNlMS00MTdjLWEwZDItZWQ0ZTRmODkwN2ExM2QzMGIwODgtMjFkYS00ZDk2LTk2NGItOGU2MjE2YzBlNTk0&
state=CfDJ8HEuPZo8hC1NjU6vRJir-D8WZp_uajNVpQxcKBeeTsf0q4Yf8fQykOIXck_1FAo-FKgarNQfBsV9BNoBkICIuTuee89MzYKClkdoUDZcanQs6rRaR6E-EFO8DtPxc6MvZci1bejm2_xV2NSiM9-y9yMrenDSRc6ul3TUgNOjnX4BIwGPfAdSAfO9k0JNVYPwRcCwzCPNJDpLxyR3uQzjqh9_KD6FZ3uRX4hJOtLkJwSakZDdE94wkEh2evubTcvciVEDdhQFnWAp0vL_yEI0qGt8m42urSi6rVHDAemd0zAU8WhCrz-mTLDXvxw-RPiUC6MIutl9HMYrRhwxTuU0kGDBbzcdf4NgJrPEkifmlNAiK2XTSWMGILmoIracKacULA&
x-client-SKU=ID_NET8_0&
x-client-ver=7.1.2.0"
Where am I going wrong?