1 using Microsoft.AspNetCore.Http;
2 using Microsoft.AspNetCore.Mvc;
3 using Microsoft.AspNetCore.Mvc.Filters;
4 using Microsoft.Extensions.Logging;
9 using System.Threading.Tasks;
42 protected ILogger Logger {
get; }
69 DatabaseContext = databaseContext ??
throw new ArgumentNullException(nameof(databaseContext));
70 if (authenticationContextFactory == null)
71 throw new ArgumentNullException(nameof(authenticationContextFactory));
72 Logger = logger ??
throw new ArgumentNullException(nameof(logger));
75 this.requireInstance = requireInstance;
76 this.requireHeaders = requireHeaders;
88 await Unauthorized().ExecuteResultAsync(context).ConfigureAwait(
false);
99 await StatusCode((
int)HttpStatusCode.UpgradeRequired,
new ErrorMessage 101 Message =
"Provided API version is incompatible with server version!" 102 }).ExecuteResultAsync(context).ConfigureAwait(
false);
110 await BadRequest(
new ErrorMessage { Message =
"Missing Instance header!" }).ExecuteResultAsync(context).ConfigureAwait(
false);
116 await Forbid().ExecuteResultAsync(context).ConfigureAwait(
false);
121 catch (InvalidOperationException e)
125 await BadRequest(
new ErrorMessage { Message = e.
Message }).ExecuteResultAsync(context).ConfigureAwait(
false);
130 if (ModelState?.IsValid ==
false)
132 var errorMessages = ModelState.SelectMany(x => x.Value.Errors).Select(x => x.ErrorMessage).ToList();
135 for (var I = 0; I < errorMessages.Count; ++I)
137 var message = errorMessages[I];
138 if (message.StartsWith(
"The ", StringComparison.Ordinal) && message.EndsWith(
" field is required.", StringComparison.Ordinal))
140 errorMessages.RemoveAt(I);
144 if (errorMessages.Count > 0)
146 await BadRequest(
new ErrorMessage { Message = String.Join(Environment.NewLine, errorMessages) }).ExecuteResultAsync(context).ConfigureAwait(
false);
156 await base.OnActionExecutionAsync(context, next).ConfigureAwait(
false);
158 catch (OperationCanceledException e)
160 Logger.LogDebug(
"Request cancelled! Exception: {0}", e);
For creating and accessing authentication contexts
Manages Api.Models.Users for a scope
ApiController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, ILogger logger, bool requireInstance, bool requireHeaders)
Construct an ApiController
override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
Use server authentication
Represents the currently authenticated Api.Models.User
InstanceUser InstanceUser
string Message
A human readable description of the error
A Controller for API functions
readonly bool requireHeaders
If ApiHeaders are required
long Id
The ID of the User
readonly bool requireInstance
If IAuthenticationContext.InstanceUser permissions are required to access the ApiController ...
Metadata about a server instance
IAuthenticationContext CurrentAuthenticationContext
The IAuthenticationContext the IAuthenticationContextFactory created
Represents an error message returned by the server