c# - 登录时Web API多重身份验证

标签 c# azure authentication asp.net-web-api2 azure-active-directory

我正在尝试确定是否可以在我们的 Web API 中使用各种身份验证机制。我们正在研究的两个是:

  • 使用用户名和密码对数据库进行身份验证(当前实现)
  • 针对 Azure AD 进行身份验证

我正在努力解决的问题是:

  • 如何配置两者。换句话说,知道在 API 中使用哪些机制
  • 其次,如何在运行时( Multi-Tenancy 站点)使用不同的 ida:ClientId (Azure AD Auth)

非常感谢!

最佳答案

我们可以直接在Web API项目中添加多个身份验证中间件。要使用 Azure AD 添加身份验证,我们可以使用 Microsoft.Owin.Security.ActiveDirectory。以下是个人帐户和 Azure AD 帐户的代码支持供您引用:

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context and user manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

    // Enable the application to use a cookie to store information for the signed in user
    // and to use a cookie to temporarily store information about a user logging in with a third party login provider
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Configure the application for OAuth based flow
    PublicClientId = "self";
    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId),
        AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        // In production mode set AllowInsecureHttp = false
        AllowInsecureHttp = true
    };

    // Enable the application to use bearer tokens to authenticate users
    app.UseOAuthBearerTokens(OAuthOptions);

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
      new WindowsAzureActiveDirectoryBearerAuthenticationOptions
      {
          Audience = ConfigurationManager.AppSettings["ida:Audience"],
          Tenant = ConfigurationManager.AppSettings["ida:Tenant"],

      });    
}

要验证本地帐户,我们可以从使用 Web API 项目构建的授权服务器获取访问 token 。对于Azure AD帐户,我们需要从Azure AD获取 token 。

How do I configure both. In other words, know which mechanisms to use in the API

在前端应用程序中,您还应该提供一个按钮来使用 Azure AD 登录,然后从 Azure AD 获取访问 token 。然后您可以使用此访问 token 作为个人帐户调用 Web API。

Secondly, how do I make use of a different ida:ClientId (Azure AD Auth) during run-time (multi tenant site)

如果您想开发 Multi-Tenancy 站点,当您在 Azure AD 上注册 Web 应用/API 应用时,我们需要启用 Multi-Tenancy 。并将授权/ token 端点中的租户替换为common。之后,其他租户的用户就可以登录您的应用程序了。有关 Multi-Tenancy 开发的更多详细信息,您可以引用以下链接:

How to sign in any Azure Active Directory (AD) user using the multi-tenant application pattern

关于c# - 登录时Web API多重身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44818347/

相关文章:

c# - 从 ContinueWith 中抛出异常

c# - 无法加载 7-zip 库或内部 COM 错误。库无效

java - 在 weblogic 12c 上部署时创建 Azure 存储容器失败

asp.net - AJAX 请求时 Azure SQL 中的间歇性连接超时

rest - Yii2 REST api 承载认证

使用网络服务的Android APP如何对用户进行身份验证

c# - 在对象图中查找具有相同键的实体以防止 "An object with the same key already exists in the ObjectStateManager"错误

c# - 如何使用NEST 7.4.1删除索引?

azure - 如何从 MVC View 将文件上传到 Azure Blob 存储

security - 使用 nonce 的电子邮件中激活/注册/密码重置链接的最佳做法是什么