未处理的拒绝(错误):无法加载“WebPortal”的设置 - ASP.NET Core React

2023-12-30

我创建了一个 ASP.NET Core 3React项目,我不断收到此错误。

未处理的拒绝(错误):无法加载“WebPortal”的设置

GET https://localhost:44367/_configuration/WebPortal https://localhost:44367/_configuration/WebPortal 401

未捕获(承诺)错误:无法加载“WebPortal”的设置

在 AuthorizeService.ensureUserManagerInitialized (AuthorizeService.js:184)
在异步 AuthorizeService.getUser (AuthorizeService.js:24)
在异步 AuthorizeService.isAuthenticated (AuthorizeService.js:15)
在异步 Promise.all(索引 0)
在异步 LoginMenu.populateState (LoginMenu.js:26)

这是弹出的错误(AuthorizeService.js):

    async ensureUserManagerInitialized() {
        if (this.userManager !== undefined) {
            return;
        }

        let response = await fetch(ApplicationPaths.ApiAuthorizationClientConfigurationUrl);

        if (!response.ok) {
            throw new Error('Could not load settings for '${ApplicationName}');
        }

        let settings = await response.json();
        settings.automaticSilentRenew = true;
        settings.includeIdTokenInSilentRenew = true;
        settings.userStore = new WebStorageStateStore({
            prefix: ApplicationName
        });

        this.userManager = new UserManager(settings);
        this.userManager.events.addUserSignedOut(async () => {
            await this.userManager.removeUser();
            this.updateState(undefined);
        });
    }

My ApiAuthorizationConstants.js file:

    export const ApplicationName = 'WebPortal';
    export const QueryParameterNames = {
      ReturnUrl: 'returnUrl',
      Message: 'message'
    };
    export const LogoutActions = {
      LogoutCallback: 'logout-callback',
      Logout: 'logout',
      LoggedOut: 'logged-out'
    };
    export const LoginActions = {
      Login: 'login',
      LoginCallback: 'login-callback',
      LoginFailed: 'login-failed',
      Profile: 'profile',
      Register: 'register'
    };
    const prefix = '/authentication';
    export const ApplicationPaths = {
       DefaultLoginRedirectPath: '/',
       ApiAuthorizationClientConfigurationUrl: '/_configuration/${ApplicationName}',
       ApiAuthorizationPrefix: prefix,
       Login: '${prefix}/${LoginActions.Login}',
       LoginFailed: '${prefix}/${LoginActions.LoginFailed}',
       LoginCallback: '${prefix}/${LoginActions.LoginCallback}',
       Register: '${prefix}/${LoginActions.Register}',
       Profile: '${prefix}/${LoginActions.Profile}',
       LogOut: '${prefix}/${LogoutActions.Logout}',
       LoggedOut: '${prefix}/${LogoutActions.LoggedOut}',
       LogOutCallback: '${prefix}/${LogoutActions.LogoutCallback}',
       IdentityRegisterPath: '/Identity/Account/Register',
       IdentityManagePath: '/Identity/Account/Manage'
     };

在控制台中,我看到:

谁能帮我?


根据@Woodz和@Jack的评论,我调查了问题并找出了问题所在。 问题是Home页面需要授权。我将在这里发布我的解决方案,它可能对某人有帮助。

问题原因

In my 启动.cs类中,我为所有控制器启用了授权。看下面的代码,

services.AddMvc(options => {

                //**************Add General Policy *********************
                //User need to be a Authorized system user to access pages except allowAnonymous annotation
                var generalPolicy = new AuthorizationPolicyBuilder()
                                           .RequireAuthenticatedUser()
                                           .Build();
                options.Filters.Add(new AuthorizeFilter(generalPolicy));
                options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());


            })

Solution

  • 更改授权于OidcConfigurationController.cs (Add 允许匿名 注解).

    [AllowAnonymous]
    public class OidcConfigurationController : Controller
    {
        private readonly ILogger<OidcConfigurationController> _logger;
    
        public OidcConfigurationController(IClientRequestParametersProvider 
          clientRequestParametersProvider, ILogger<OidcConfigurationController> 
         logger)
        {
            ClientRequestParametersProvider = clientRequestParametersProvider;
            _logger = logger;
         }
    
        public IClientRequestParametersProvider ClientRequestParametersProvider 
          { get; }
    
        [HttpGet("_configuration/{clientId}")]
        public IActionResult GetClientRequestParameters([FromRoute]string clientId)
        {
            var parameters = 
                     ClientRequestParametersProvider.GetClientParameters(HttpContext, 
                     clientId);
            return Ok(parameters);
        }
    }
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未处理的拒绝(错误):无法加载“WebPortal”的设置 - ASP.NET Core React 的相关文章

随机推荐