question

ArthurOchoa-3752 avatar image
0 Votes"
ArthurOchoa-3752 asked KellieBroxton-1365 published

HELP! How do you REDIRECT to an external site outside a Razor Component using HttpContext.Response.Redirect


GIVEN: This is a Blazor Server Project

WHAT WORKS: HttpContext works when you inject IHttpContextAccessor inside the Razor component. Inside my Index.razor file, I have the following:

 @page "/"
 @using Microsoft.AspNetCore.Http
 @inject IHttpContextAccessor httpContextAccessor
    
 <p>Welcome to your new app.</p>
    
 @code {
    
     protected override void OnInitialized()
     {
         httpContextAccessor.HttpContext.Response.Redirect("https://www.site1.com/auth" + "?return_url=" + "https://www.mysite.com/blazorapp");
     }
 }

THE ISSUE: Access to HttpContext outside a Razor component returns a null value (i.e. in a separate Class file).

WHAT I HAVE DONE SO FAR:

I added on Startup.cs

         services.AddHttpContextAccessor();
         services.AddScoped<HttpContextAccessor>();

I created a separate class (MyBase.cs):

 using System;
 using Microsoft.AspNetCore.Components;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.DependencyInjection;
    
 namespace BlazorApp.Server.Pages
 {
     public class MyBase : ComponentBase
     {
         private IHttpContextAccessor _httpContextAccessor;
         private IServiceProvider _services;
    
         public MyBase(IHttpContextAccessor httpContextAccessor)
         {
             _httpContextAccessor = httpContextAccessor;
         }
        public void AuthenticateSite()
        {
             _httpContextAccessor = _services.GetRequiredService<IHttpContextAccessor>();
             _httpContextAccessor.HttpContext.Response.Redirect("https://www.site1.com/auth" + "?return_url=" + "https://www.mysite.com/blazorapp");
        }
     }
 }

Then, I added the following in my Index.razor file:

 @page "/"
 @inherit MyBase
 @inject IHttpContextAccessor httpContextAccessor

 @code
 {
     protected override void OnInitialized()
     {
         MyBase myBase = new MyBase(httpContextAccessor);
         myBase.AuthenticateSite();
     }
 }

THE GOAL: I would like to have a separate class to handle the RESPONSE.REDIRECT.

I'm new to Blazor development, anyone with some idea or technical expertise who can help me with the issue is greatly appreciated. Thanks in advance.


















dotnet-aspnet-core-blazor
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Blazor applications run in the browser. There's no context except the first time the application is downloaded. According to your redirect, it looks like you want to use a remote auth server. The SDK comes with an OAuth template. Checkout the links on the left for other options. For example, a hosted configuration with Identity Server.


1 Vote 1 ·

Hi @AgaveJoe , thanks for the feedback.

Question, you are only referring to Blazor apps right when you said "context is only available the first time the app is downloaded."? Is that the rule only for Blazor?

All I'm doing is to execute a Response.Redirect inside a class.

Have not tried OAuth template, thanks for providing me this info.

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe ArthurOchoa-3752 ·

Question, you are only referring to Blazor apps right when you said "context is only available the first time the app is downloaded."? Is that the rule only for Blazor?

Blazor is like any other SPA. You have the same situation with Angular, React, Vue, etc.

Blazor server is running in the browser and it uses SignalR send/receive messages. It is possible to get the context one time when the application is first loads which is why your initial code seems to work. After the initial load there are no more HTTP requests only SignalR messages.










1 Vote 1 ·
Show more comments

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered KellieBroxton-1365 published

Hi @ArthurOchoa-3752,

THE ISSUE: Access to HttpContext outside a Razor component returns a null value (i.e. in a separate Class file).

Please set a break point in the AuthenticateSite() method, then debug your code, the issue relates the IServiceProvider, the _services is null. So, it will throw the ArgumentNullException: Value cannot be null. (Parameter 'provider' error.

Try to comment or remove _services relate code, the redirect action should work.

     public void AuthenticateSite()
     { 
           //_httpContextAccessor = _services.GetRequiredService<IHttpContextAccessor>();
          _httpContextAccessor.HttpContext.Response.Redirect("https://www.site1.com/auth" + "?return_url=" + "https://www.mysite.com/blazorapp");
     }


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,
Dillion

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @ZhiLv-MSFT, thanks for your feedback. Will give your recommendation a try.

0 Votes 0 ·
ZhiLv-MSFT avatar image ZhiLv-MSFT ArthurOchoa-3752 ·

Hi @ArthurOchoa-3752,

May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·

Hi @ZhiLv-MSFT , thanks for the follow-up.

To answer your question, YES and NO. I tried your recommendation, unfortunately it did not work. However, I had to go through some steps to make it work, but it was not a straight forward...

0 Votes 0 ·

Will post the solution soon.

0 Votes 0 ·
Show more comments