.net 核心中的混合身份验证与 Open Id Connect 和本地数据库

Hybrid authentication in .net core with Open Id Connect and local database(.net 核心中的混合身份验证与 Open Id Connect 和本地数据库)
本文介绍了.net 核心中的混合身份验证与 Open Id Connect 和本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否有一种模式来设计一个能够同时使用 Open Id Connect(在 Azure AD 中连接)和本地数据库对用户进行身份验证的应用程序?

Is there a pattern to design an app who's cappable of authenticate users with both Open Id Connect (connected in Azure AD) and a local database?

我正在创建的应用程序将拥有来自拥有 Azure Active Directory 的公司的用户,但也有未受雇于该公司的用户必须使用该应用程序,因为他们未在 Azure AD 中注册.

The app I'm creating will have users from a company that does has an Azure Active Directory, but also has users not employed by said company who must use the app since they are not registred in Azure AD.

没有 Azure AD 的身份验证方法应该使用本地数据库,而不是其他身份验证提供程序.

The authentication method without the Azure AD should use a local database, not other authentication providers.

推荐答案

您可以使用 ASP.NET Identity 来管理数据库中的本地用户,并使用 Azure AD 作为外部身份提供者,使 AAD 帐户能够登录您的应用程序.您可以识别 Azure AD 用户并链接到本地数据库中的用户,以便您还可以管理与本地用户和 Azure AD 用户的关系/角色.

You can use ASP.NET Identity for managing your local users in database ,and use Azure AD as external identity provider which enable the AAD accounts to login in your application . You can identify the Azure AD user and link to a user in your local DB , so that you can also manage relationship/roles both with your local users and Azure AD users .

我将提供一个简单的代码示例来说明如何实现该功能:

I will provide a simple code sample for how to implement that feature :

  1. 使用 ASP.NET Identity(Individual User Accounts 模板)创建新的 .net 核心应用程序.

  1. Create new .net core application with ASP.NET Identity (Individual User Accounts template).

安装包:Microsoft.AspNetCore.Authentication.AzureAD.UI

Install the package : Microsoft.AspNetCore.Authentication.AzureAD.UI

修改 Startup.cs 以启用 Azure AD 身份验证:

Modify the Startup.cs to enable Azure AD Authentication:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

services.AddAuthentication(sharedOptions =>
{

}).AddAzureAD(options => Configuration.Bind("AzureAd", options)).AddCookie();

  • 修改 appsettings.json 以添加 Azure AD 应用设置:

  • Modify the appsettings.json to add the Azure AD app settings:

    "AzureAd": {
        "Instance": "https://login.microsoftonline.com/",
        "Domain": "xxx.onmicrosoft.com",
        "TenantId": "xxxxxx-xxxxx-4f08-b544-b1eb456f228d",
        "ClientId": "xxxxx-xxxxx-4717-9821-e4f718fbece4",
        "CallbackPath": "/signin-oidc",
        "CookieSchemeName": "Identity.External"
    },
    

    用户在登录过程中可以选择本地用户或AAD用户登录.

    Users could choose login with local user or AAD user during the login process .

    这篇关于.net 核心中的混合身份验证与 Open Id Connect 和本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
  • 相关文档推荐

    The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
    Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
    Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
    Build project with Microsoft.Build API(使用 Microsoft.Build API 构建项目)
    Add a msbuild task that runs after building a .NET Core project in Visual Studio 2017 RC(添加在 Visual Studio 2017 RC 中构建 .NET Core 项目后运行的 msbuild 任务)
    Assembly binding error when building Office add-in: quot;FindRibbonsquot; task failed unexpectedly(构建 Office 加载项时的程序集绑定错误:“FindRibbons任务意外失败)