首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在同一Web应用程序中使用Cofoundry和个人用户帐户身份验证和授权时的问题

在同一Web应用程序中使用Cofoundry和个人用户帐户身份验证和授权时的问题
EN

Stack Overflow用户
提问于 2020-12-05 02:45:10
回答 1查看 31关注 0票数 0

如果我添加

代码语言:javascript
复制
services.AddControllersWithViews()
        .AddCofoundry(Configuration);

对于我的Startup.cs,我的认证和授权失败。

如果我在Startup.cs中禁用上述代码行,则@if (SignInManager.IsSignedIn(User))为true,但是如果我取消注释它,则@if (SignInManager.IsSignedIn(User))始终为false,尽管用户已登录。

不能在同一个应用程序中使用授权、身份验证和Cofoundry吗?

EN

回答 1

Stack Overflow用户

发布于 2020-12-05 03:45:26

Cofoundry有自己的用户管理系统,并自动向管道添加身份验证以支持这一点。有关如何创建您自己的自定义用户区域的信息,请参阅User Area docs

目前还没有太多支持在运行Cofoundry的同时运行自己的身份验证机制(计划进行改进),但是您可以通过实现自己的IAuthConfiguration实现(包括DefaultAuthConfiguration中的代码)覆盖现有的身份验证注册,并根据需要对其进行自定义,以确保正确配置Cofoundry。例如:

代码语言:javascript
复制
using Cofoundry.Core.DependencyInjection;
using Cofoundry.Domain;
using Cofoundry.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;

/// <summary>
/// Use an IDependencyRegistration instance to override the default implementation
/// </summary>
public class AuthDependencyRegistration : IDependencyRegistration
{
    public void Register(IContainerRegister container)
    {
        container.Register<IAuthConfiguration, CustomAuthConfiguration>(RegistrationOptions.Override());
    }
}

/// <summary>
/// This is a copy of the default config
/// </summary>
public class CustomAuthConfiguration : IAuthConfiguration
{
    private readonly IUserAreaDefinitionRepository _userAreaDefinitionRepository;
    private readonly IAuthCookieNamespaceProvider _authCookieNamespaceProvider;

    public CustomAuthConfiguration(
        IUserAreaDefinitionRepository userAreaDefinitionRepository,
        IAuthCookieNamespaceProvider authCookieNamespaceProvider
        )
    {
        _userAreaDefinitionRepository = userAreaDefinitionRepository;
        _authCookieNamespaceProvider = authCookieNamespaceProvider;
    }

    public void Configure(IMvcBuilder mvcBuilder)
    {
        var services = mvcBuilder.Services;
        var allUserAreas = _userAreaDefinitionRepository.GetAll();

        // Set default schema as specified in config, falling back to CofoundryAdminUserArea
        // Since any additional areas are configured by the implementor there shouldn't be multiple
        // unless the developer has misconfigured their areas.
        var defaultSchemaCode = allUserAreas
            .OrderByDescending(u => u.IsDefaultAuthSchema)
            .ThenByDescending(u => u is CofoundryAdminUserArea)
            .ThenBy(u => u.Name)
            .Select(u => u.UserAreaCode)
            .First();

        var defaultScheme = CofoundryAuthenticationConstants.FormatAuthenticationScheme(defaultSchemaCode);

        var authBuilder = mvcBuilder.Services.AddAuthentication(defaultScheme);
        var cookieNamespace = _authCookieNamespaceProvider.GetNamespace();

        foreach (var userAreaDefinition in allUserAreas)
        {
            var scheme = CofoundryAuthenticationConstants.FormatAuthenticationScheme(userAreaDefinition.UserAreaCode);

            authBuilder
                .AddCookie(scheme, cookieOptions =>
                {
                    cookieOptions.Cookie.Name = cookieNamespace + userAreaDefinition.UserAreaCode;
                    cookieOptions.Cookie.HttpOnly = true;
                    cookieOptions.Cookie.IsEssential = true;
                    cookieOptions.Cookie.SameSite = SameSiteMode.Lax;

                    if (!string.IsNullOrWhiteSpace(userAreaDefinition.LoginPath))
                    {
                        cookieOptions.LoginPath = userAreaDefinition.LoginPath;
                    }
                });
        }

        mvcBuilder.Services.AddAuthorization();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65149043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档