未处理的拒绝(错误):无法加载 'WebPortal' - ASP.NET Core React 的设置
Unhandled Rejection (Error): Could not load settings for 'WebPortal' - ASP.NET Core React
我创建了一个 ASP.NET Core 3 React 项目,但我一直收到此错误。
Unhandled Rejection (Error): Could not load settings for 'WebPortal'
GET https://localhost:44367/_configuration/WebPortal 401
Uncaught (in promise) Error: Could not load settings for 'WebPortal'
at AuthorizeService.ensureUserManagerInitialized (AuthorizeService.js:184)
at async AuthorizeService.getUser (AuthorizeService.js:24)
at async AuthorizeService.isAuthenticated (AuthorizeService.js:15)
at async Promise.all (index 0)
at async 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);
});
}
我的 ApiAuthorizationConstants.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 的评论,我调查了这个问题并找出了问题所在。
问题是 主页 需要页面 authorization.I 将 post 我的解决方案放在这里,它可能对某些人有帮助。
问题原因
在我的 Startup.cs class 中,我为所有控制器启用了授权。见下面的代码,
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());
})
解决方案
更改OidcConfigurationController.cs中的授权(添加AllowAnonymousannotation).
[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);
}
}
我创建了一个 ASP.NET Core 3 React 项目,但我一直收到此错误。
Unhandled Rejection (Error): Could not load settings for 'WebPortal'
GET https://localhost:44367/_configuration/WebPortal 401
Uncaught (in promise) Error: Could not load settings for 'WebPortal'
at AuthorizeService.ensureUserManagerInitialized (AuthorizeService.js:184)
at async AuthorizeService.getUser (AuthorizeService.js:24)
at async AuthorizeService.isAuthenticated (AuthorizeService.js:15)
at async Promise.all (index 0)
at async 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);
});
}
我的 ApiAuthorizationConstants.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 的评论,我调查了这个问题并找出了问题所在。 问题是 主页 需要页面 authorization.I 将 post 我的解决方案放在这里,它可能对某些人有帮助。
问题原因
在我的 Startup.cs class 中,我为所有控制器启用了授权。见下面的代码,
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());
})
解决方案
更改OidcConfigurationController.cs中的授权(添加AllowAnonymousannotation).
[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); } }