Giter Site home page Giter Site logo

blazorwebasmeasyauth's Introduction

Blazor WebAsm Easy Auth

Use EasyAuth of Azure Function App from Blazor WebAssembly application.

Usage

Configure App Service

Configure Blazor WebAssembly application

DI for EasyAuth settings.

public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");

    builder.Services.AddEasyAuth(new EasyAuthConfig()
    {
        BlazorWebsiteURL = "https://<client side blazor site url>",
        AzureFunctionAuthURL = "https://<your Azure function name>.azurewebsites.net",   
    });

    await builder.Build().RunAsync();
}

To log in, go to the login URL for each EazyAuth ID provider.

To log out, call the EasyAuthAuthenticationStateProvider's Logout method. State changes are notified by the provider.

@inject NavigationManager _navigationManager
@inject EasyAuthNavigationHelper _navigationHelper
@inject EasyAuthAuthenticationStateProvider _easyAuthAuthenticationStateProvider

<AuthorizeView>
    <Authorizing>
        <NavLink class="nav-link" href="">Authenticating</NavLink>
    </Authorizing>
    <NotAuthorized>
        <NavLink class="nav-link" href="" @onclick="@LoginWithTwitter">
            Sign In
        </NavLink>
    </NotAuthorized>
    <Authorized>
        <NavLink class="nav-link" href="">
            <span>@context.User.Identity.Name</span>
        </NavLink>
        <NavLink class="nav-link" href="" @onclick="@Logout">
            Sign Out
        </NavLink>
    </Authorized>
</AuthorizeView>

@code {
    private void LoginWithTwitter()
    {
        _navigationManager.NavigateTo(_navigationHelper.GetLoginUrl(EasyAuthProvider.Twitter));
    }

    private async void Logout()
    {
        await _easyAuthAuthenticationStateProvider.Logout();
    }
}

And you can get HttpClient with Zumo authentication header set.

@attribute [Authorize]
@inject EasyAuthAuthenticationStateProvider _easyAuthAuthenticationStateProvider

@code {
    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override async Task OnInitializedAsync()
    {
        // Currently, it is necessary to wait for confirmation of the authentication status.
        await authenticationStateTask;
        var httpClient = _easyAuthAuthenticationStateProvider.GetZumoAuthedClientAsync();
    }
}

The rest is the same as other AuthenticationStateProvider.

See also Blazor authentication and authorization document. https://docs.microsoft.com/en-us/aspnet/core/security/blazor/

Thanks

This library was a great reference for this article.

https://medium.com/@hurtertyjoil/client-side-blazor-authentication-with-azure-functions-and-easyauth-c454faad657b

blazorwebasmeasyauth's People

Contributors

7474 avatar

Watchers

James Cloos avatar  avatar  avatar

blazorwebasmeasyauth's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.