Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IRestClient given by IRequestPreparation Prepare method throws Progress.Sitefinity.RestSdk.Exceptions.ErrorCodeException: The current user is not allowed access #157

Open
chenyuAC opened this issue Dec 19, 2024 · 3 comments

Comments

@chenyuAC
Copy link

None of the examples here make use of the IRestClient passed in

public Task Prepare(PageModel pageModel, IRestClient restClient, HttpContext context)

public async Task Prepare(PageModel pageModel, IRestClient restClient, HttpContext context)

When I attempt to use it to query any content type data, it throws

System.AggregateException: One or more errors occurred. (The current user is not allowed access)
 ---> Progress.Sitefinity.RestSdk.Exceptions.ErrorCodeException: The current user is not allowed access
   at Progress.Sitefinity.RestSdk.Client.RestClient.SendMessage[T](RequestData requestData)
   at Progress.Sitefinity.RestSdk.Client.RestClient.GetItems[T](GetAllArgs args)
   at Renderer.Services.RequestPreparations.Prepare(PageModel pageModel, IRestClient restClient, HttpContext context)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.WaitAllCore(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks)
   at Progress.Sitefinity.AspNetCore.Preparations.PreparationsInitializer.RunCustomRequests(IRestClient restClient, RequestArgs requestArgs, PageModel pageModel, HttpContext httpContext)
   at Progress.Sitefinity.AspNetCore.Preparations.PreparationsInitializer.Initialize(IRestClient restClient, PageModel pageModel, HttpContext httpContext)
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.InitRestClient(HttpContext httpContext, String metadataHash, RequestContext requestContext, IRenderContext renderContext)
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.HandlePageModelResponse(HttpContext httpContext, HttpResponseMessage httpResponseMessage, Func`1 next)
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.TryHandleResponse(HttpResponseMessage httpResponseMessage, HttpContext httpContext, Func`1 next, Boolean disabledProxy)
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.SendHttpRequestMessage(HttpRequestMessage httpRequestMessage, Uri forwardHost, HttpClient httpClient, HttpContext context, Func`1 next, Func`2 sendRequest, ISitefinityConfig sitefinityConfig)
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.SendHttpRequestMessage(HttpRequestMessage httpRequestMessage, Uri forwardHost, HttpClient httpClient, HttpContext context, Func`1 next, Func`2 sendRequest, ISitefinityConfig sitefinityConfig)
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.<>c__DisplayClass6_0.<<TryHandleRequest>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Progress.Sitefinity.AspNetCore.Http.Proxy.DefaultProxyHandler.TryHandleRequest(HttpContext context, Func`1 next)
   at Progress.Sitefinity.AspNetCore.Areas.Diagnostics.Profiling.ProxyMiddleware.Invoke(HttpContext context)
   at Progress.Sitefinity.AspNetCore.Areas.Cache.ExternalCacheMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.OutputCaching.OutputCacheMiddleware.InvokeAwaited(HttpContext httpContext, IReadOnlyList`1 policies)
   at Progress.Sitefinity.AspNetCore.ResourcePackages.ResourcePackagesMiddleware.Invoke(HttpContext context)
   at Progress.Sitefinity.AspNetCore.Web.UI.EmbeddedResourceMiddleware.Invoke(HttpContext context)
   at Progress.Sitefinity.AspNetCore.Areas.Diagnostics.Profiling.DiagnosticsMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Is the IRestClient working here? If I use the IRestClient from dependency injection as following it works

public class RequestPreparations(ILogger<RequestPreparations> logger, IRestClient restClientDI) : IRequestPreparation

I have WebServiceApiKey configured, is it related?

@Novkirishki
Copy link
Contributor

Hello @chenyuAC,

Thank you for contacting us about this.
The IRestClient that comes as a parameter in the Prepare method of the IRequestPreparation is in fact a batch client. This means it accumulates requests from all preparations and executes them at once. Therefore the result from this client should be anticipated asynchronously (for example by using ContinueWith()).

I have updated the sample to showcase this: PR

Do not hesitate to contact us in case you have further questions or issues.

Best,
Niki

@chenyuAC
Copy link
Author

chenyuAC commented Jan 3, 2025

@Novkirishki Thank you for updating the example.

I have a related question that I hope you can assist with. Is it possible to retrieve draft content items, such as in preview mode?

It seems that the built-in ContentList Widget from https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-widgets)](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-widgets utilizes the requestContext.ResolvedDetailItem, as implemented here: https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-widgets/blob/2a5bee615e8e93f2a0727c3afdf11aa07c17a02b/Progress.Sitefinity.AspNetCore.Widgets/Models/ContentList/ContentListModelForDetail.cs#L93

Is it possible to access this item (especially in draft state) in an IRequestPreparation implementation? I am encountering challenges querying items in draft state within our custom master-detail widget implementation using IRequestPreparation.

@Novkirishki
Copy link
Contributor

Novkirishki commented Jan 3, 2025

The default restClient uses the web service that is set in the appsettings.json file. By default it is 'api/default'. This is a frontend web service and therefore returns only live items and not drafts.

In order to fetch draft items you should use a backend web service such as 'sf/system'.
You can check this.

Another alternative is to create a custom endpoint that returns the desired item: Docs

You can contact Sitefinity support for more info.

Best,
Niki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants