使用Microsoft Identity Web应用程序和Microsoft.aspnetcore.Sidentity在同一Web项目中使用的问题

发布于 2025-01-28 04:38:01 字数 1568 浏览 4 评论 0原文

在我当前的应用中,我需要使用两种不同的身份验证:

  1. Microsoft.aspnetcore.distity-具有内部身份表 客户用户
  2. Microsoft Identity Web应用程序 - 内部的Azure AD身份验证 我的组织的用户

如果我尝试仅配置一项服务,则它可以完美地工作。当我添加Services Microsoft.aspnetcore.Identity和Microsoft Identity Web应用程序中时,问题就会发生。然后我的一种身份验证停止工作。

例如。如果我将这两个代码添加在一起,那么在工作和内部符号中的Azure符号不起作用:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
options =>
{
    options.SignIn.RequireConfirmedAccount = true;
    options.Password.RequiredLength = 8;
    options.Password.RequireDigit = true;
    options.SignIn.RequireConfirmedEmail = true;
}
)
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("Authentication:AzureAd"));

,但是如果我将这两个代码添加在一起,则在工作中的内部符号,但是Azure AD符号却无效:< /strong>

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Password.RequiredLength = 8;
options.Password.RequireDigit = true;
options.SignIn.RequireConfirmedEmail = true;
}
)
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

builder.Services.AddAuthentication()
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("Authentication:AzureAd"));

尽管在这两种情况下,该过程都没有错误的验证,但似乎没有在对象系统上填写索赔。因此,最终就像没有经过身份验证(尽管它们是)。

以前有人经历过吗?知道如何解决吗?

谢谢!

In my currently application I need to use two different authentications:

  1. Microsoft.AspNetCore.Identity - with internal Identity tables for
    Customer users
  2. Microsoft Identity Web App - Azure AD authentication for Internal
    users of my Organization

If I try to configure ONLY ONE service it works perfectly. The problem happens when I add the services Microsoft.AspNetCore.Identity and Microsoft Identity Web App to the same application. Then my one of the authentication stops working.

For instance. If I add these two codes together, then Azure Sign In works and Internal Sign In does not work:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
options =>
{
    options.SignIn.RequireConfirmedAccount = true;
    options.Password.RequiredLength = 8;
    options.Password.RequireDigit = true;
    options.SignIn.RequireConfirmedEmail = true;
}
)
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("Authentication:AzureAd"));

But if I add these two codes together, then the internal Sign In works but the Azure AD Sign in does not work:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Password.RequiredLength = 8;
options.Password.RequireDigit = true;
options.SignIn.RequireConfirmedEmail = true;
}
)
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

builder.Services.AddAuthentication()
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("Authentication:AzureAd"));

Although in both cases the process authenticates with no error, it seems that the claims are not being filled on the object System.Security.Claims.ClaimsIdentity properly. So in the end is like it is not authenticated (although they are).

Has anybody experienced this before? Any idea how to solve it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2025-02-04 04:38:01

Fortunatelly我可以找到答案。

我在此Web项目中使用了Blazor Server。

解决此问题的解决方法是更改​​授权属性的某些配置。因此,基本上此代码一直以这种方式:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
       options =>
       {
               options.SignIn.RequireConfirmedAccount = true;
               options.Password.RequiredLength = 8;
               options.Password.RequireDigit = true;
               options.SignIn.RequireConfirmedEmail = true;                                        
        }
)            
.AddEntityFrameworkStores<DbContext>()            
.AddDefaultTokenProviders();

builder.Services.AddAuthentication()
.AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.Bind("Authentication:AzureAd", options);        
    }
);

我是否替换了此代码:

app.MapBlazorHub();

通过此代码:

app.MapBlazorHub()
    .AllowAnonymous()
    .RequireAuthorization(
        new AuthorizeAttribute
        {
            AuthenticationSchemes = $"{OpenIdConnectDefaults.AuthenticationScheme},{IdentityConstants.ApplicationScheme}",        
        }
   );

它现在非常工作。

Fortunatelly I could find the answer.

Curently I am using Blazor Server in this Web Project.

A workaround to solve this problem was to change some configuration of the Authorize attribute. So, basically this code stays this way:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
       options =>
       {
               options.SignIn.RequireConfirmedAccount = true;
               options.Password.RequiredLength = 8;
               options.Password.RequireDigit = true;
               options.SignIn.RequireConfirmedEmail = true;                                        
        }
)            
.AddEntityFrameworkStores<DbContext>()            
.AddDefaultTokenProviders();

builder.Services.AddAuthentication()
.AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.Bind("Authentication:AzureAd", options);        
    }
);

Am I replaced this code:

app.MapBlazorHub();

By this code:

app.MapBlazorHub()
    .AllowAnonymous()
    .RequireAuthorization(
        new AuthorizeAttribute
        {
            AuthenticationSchemes = 
quot;{OpenIdConnectDefaults.AuthenticationScheme},{IdentityConstants.ApplicationScheme}",        
        }
   );

It works prefectly now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文