External Identity Provider with ASP.NET Core Identity - Code Maze (code-maze.com)

发布时间 2023-07-12 16:51:49作者: ChuckLu

External Identity Provider with ASP.NET Core Identity - Code Maze (code-maze.com)

Using an external identity provider while login to the application is a quite common case. This enables us to log in with our external accounts like Google, Facebook, etc. By using ASP.NET Core Identity, we are going to see that this is not a hard process at all.

So, in this article, we are going to learn how to configure an external identity provider in our ASP.NET Core application and how to use a Google account to log in to our application. Of course, in a very similar way, you can configure any other external account.

One important thing to know here. Once an external user logs in to our system, they will always bring an identifier that is unique for that user in our system. That user could have different Ids for different sites but for our site, that Id will always be the same.

To download the source code for this project, you can visit the External Identity Provider with ASP.NET Core Identity repository.

To navigate through the entire series, visit the ASP.NET Core Identity series page.

So, let’s get on it.

 

 

Now, let’s create the _ExternalAuthentication partial view, to support external authentication

@using Microsoft.AspNetCore.Identity
@using IdentityByExamples.Models

@inject SignInManager<User> SignInManager

<div class="col-md-4 offset-2">
    <section>
        <h4>Use different service for log in:</h4>
        <hr />
        @{
            var providers = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (!providers.Any())
            {
                <div>
                    <p>
                        We couldn't find any external provider
                    </p>
                </div>
            }
            else
            {
                <form asp-action="ExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
                    <div>
                        <p>
                            @foreach (var provider in providers)
                            {
                                <input type="submit" class="btn btn-info" value="@provider.Name" name="provider" />
                            }
                        </p>
                    </div>
                </form>
            }
        }
    </section>
</div>

By using the SignInManager.GetExternalAuthenticationSchemesAsync method, we fetch all the registered providers in our application. And if we find any, we show it in the view. So, in order to see these changes, let’s just include this partial view in the Login view:

<div class="col-md-4">
    <form asp-action="Login" asp-route-returnUrl="@ViewData["ReturnUrl"]">
          //code removed for clarity reasons  
    </form>
</div>
<partial name="_ExternalAuthentication" />

Now, if we navigate to the Login view:

This looks great. Now, we can implement actions in the Account controller.

 

 

 

Conclusion

So, that’s all it takes to configure and integrate an external identity provider into our ASP.NET Core application.

To sum up. We have learned:

  • How to configure our project with the Google API
  • The way to configure External Identity Provider in our application
  • How to implement External Identity Provider with actions and views

We hope you have enjoyed this article and the complete series as well.