Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication for serverside blazor #15533

Closed
floreseken opened this issue Oct 12, 2018 · 13 comments
Closed

Authentication for serverside blazor #15533

floreseken opened this issue Oct 12, 2018 · 13 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@floreseken
Copy link

I've read this: https://github.com/aspnet/Blazor/issues/1449
and this: https://github.com/aspnet/Blazor/issues/698

And still have some questions.
The last option (of 1449) kind of defeats the purpose of SS blazor in my opinion. This way you are losing lots of the benefits. (creating a API surface and client), but that will work.

And the first option can only be used to control access to the whole application, right? Not to identify users in the components code.

For instance, I would like to be able to use:

Thread.CurrentPrincipal

in my components, but enabling authentication(in the server part) does not provide this (always null). I kind of understand why.. the SignalR stuff bypasses everything of the default stack, and provides no auth bearer or something, I guess?

In my opinion being able to use a principal in serverside blazor essential for enterprise applications.
Am I missing something? Or is this not possible yet? And is my scenario covered by 698?

Thanks

@floreseken floreseken changed the title Authenication for serverside blazor Authentication for serverside blazor Oct 12, 2018
@ghost
Copy link

ghost commented Oct 12, 2018

@floreseken, You can use IHttpContextAccessor to send the current HttpContext to blazor.Client.

@ghost
Copy link

ghost commented Oct 12, 2018

blazor.Sever to Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddServerSideBlazor<Client.Startup>();
	
    // HttpContextAccessor
    services.AddHttpContextAccessor();
    services.AddScoped<HttpContextAccessor>();
}

blazor.Shared

public class HttpContextAccessor
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public HttpContextAccessor(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public HttpContext Context => _httpContextAccessor.HttpContext;
}

blazor.Client to App.cshtml

@inject blazor.Shared.HttpContextAccessor HttpContext
<Router AppAssembly=typeof(Program).Assembly />

@functions 
{      
    protected override void OnInit()
    {
        HttpContext.Context.Request.Cookies.**
		
        // Or data passed through middleware in blazor.Server
        HttpContext.Context.Features.Get<T>()
    }
}

@ghost
Copy link

ghost commented Oct 12, 2018

1) In blazor.Shared you will need to add the package Microsoft.AspNetCore.Http via nuget
2) @inject HttpContextAccessor HttpContext can be used in any .cshtml or .cs file blazor.Client

@ADefWebserver
Copy link

Also see a GitHub example at: https://github.com/SQL-MisterMagoo/BlazorTest

@carlosalcantara2668
Copy link

it worked for me !!!

@carlosalcantara2668
Copy link

@cores-system , @floreseken ,
it worked for me !!!,
I have access to all the methods of the object.

@ghost
Copy link

ghost commented Oct 13, 2018

@carlosalcantara2668, I'm glad I helped )

@roboriaan
Copy link

I was wondering if it would be possible to use [Authorize] in Blazor 0.6.0 Server-side. Something like :

In FetchData.cshtml

@using RC.App.Services
**@[Authorize]**
@page "/fetchdata"
@inject WeatherForecastService ForecastService

Thanks for the great .net core stuff guys!

@danroth27
Copy link
Member

I think we have several issues open at this point tracking the need for guidance and support for authentication and authorization, so I'm going to close this one as a duplicate of #5472.

@ADefWebserver
Copy link

I created a sample and a Blog post using the code provided above: A Demonstration of Simple Server-side Blazor Cookie Authentication

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

9 participants