Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未处理的拒绝(错误):无法加载';网站门户';-ASP.NET核心组件_Asp.net_Reactjs_Asp.net Core_Asp.net Core Identity - Fatal编程技术网

未处理的拒绝(错误):无法加载';网站门户';-ASP.NET核心组件

未处理的拒绝(错误):无法加载';网站门户';-ASP.NET核心组件,asp.net,reactjs,asp.net-core,asp.net-core-identity,Asp.net,Reactjs,Asp.net Core,Asp.net Core Identity,我创建了一个ASP.NET Core 3React项目,并且不断出现此错误 未处理的拒绝(错误):无法加载“WebPortal”的设置 拿到401 未捕获(承诺中)错误:无法加载“WebPortal”的设置 在AuthorizeService.ensureUserManagerInitialized(AuthorizeService.js:184) 在async AuthorizeService.getUser(AuthorizeService.js:24) 在async AuthorizeSe

我创建了一个ASP.NET Core 3React项目,并且不断出现此错误

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

拿到401

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

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

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

我的
apiaAuthorizationConstants.js
文件:

    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的评论,我调查了这个问题并找出了问题所在。 问题是主页页面需要授权。我将在这里发布我的解决方案,它可能会对某人有所帮助

问题原因

在Startup.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());


            })
解决方案

  • 更改OIDCConfiguration Controller.cs中的授权(添加AllowAnonymous注释)

    [AllowAnonymous]
    公共类OIDCConfiguration控制器:控制器
    {
    专用只读ILogger\u记录器;
    公共OIDCConfiguration Controller(IClientRequestParametersProvider
    clientRequestParametersProvider,ILogger
    记录器)
    {
    ClientRequestParametersProvider=ClientRequestParametersProvider;
    _记录器=记录器;
    }
    公共IClientRequestParametersProvider客户端ParametersProvider
    {get;}
    [HttpGet(“_配置/{clientId}”)]
    公共IActionResult GetClientRequestParameters([FromRoute]字符串clientId)
    {
    变量参数=
    ClientRequestParametersProvider.GetClientParameters(HttpContext,
    clientId);
    返回Ok(参数);
    }
    }
    

最好先将问题缩小到客户端或服务器,以了解哪些代码出现故障。从响应中,我们可以看到失败是由后端服务器返回的HTTP 401响应引起的。请调查后端返回此响应的原因,如果无法解决此问题,请为后端打开一个新问题。从外观上看,401表示未经授权。您是否传递了适当的令牌以让后端知道您是谁?_configuration/WebPortal是否需要经过身份验证的用户才能调用?正如其他评论所建议的那样,为401错误添加网络跟踪的原始请求/响应是值得的。
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());


            })
[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);
    }
}