Я строю сервер для проверки концепции Webdav с хранилищем SQL, а не физическим на дисковом хранилище в Framework ASP.NET. Этот сервер должен интегрироваться с существующим приложением ASP.NET Framework. Я сам реализую логику WebDAV. Я смог интегрировать сервер с моим существующим приложением, и он работал. Проблема, с которой я сталкиваюсь сейчас, заключается в том, что в методе Get/Head есть какой -то конфликт в методе Get/Head. Офис отрицает мой доступ к файлу. Если я удалю пользовательские токены, программа работает просто отлично. < /P>
#region GET / HEAD
private async Task HandleGetOrHead( HttpContext context, string path, bool headOnly )
{
if ( Guid.TryParse( context.Request.Params["token"], out var token ) )
{
await HandleGetOrHeadWithToken( context, path, token, headOnly );
return;
}
if ( !headOnly && !AssertAuthenticated( context ) ) return;
if ( !headOnly )
{
var item = await GetItemByPathAsync( path );
if ( item?.Content == null )
{
context.Response.StatusCode = 404;
context.Response.End( );
return;
}
await context.DeferredVoidAction( "RefreshToken", x => x.RefreshToken( item.ItemId ) );
return;
}
await context.Response.FlushAsync( );
context.Response.End( );
}
private async Task HandleGetOrHeadWithToken( HttpContext context, string path, Guid tokenId, bool headOnly )
{
var token = await service.GetTokenAsync( tokenId );
if ( token == null && !AssertAuthenticated( context ) ) return;
var item = await GetItemByPathAsync( path );
if ( item?.Content == null )
{
context.Response.StatusCode = 404;
context.Response.End( );
return;
}
context.Response.StatusCode = 200;
context.Response.ContentType = item.ContentType ?? "application/octet-stream";
context.Response.AddHeader( "ETag", $"\"{item.Modified.Ticks}-{item.SerialNumber ?? 0}\"" );
context.Response.AddHeader( "Last-Modified", item.Modified.ToString( "R" ) );
context.Response.AddHeader( "Content-Length", item.Content.Length.ToString( ) );
if ( !headOnly )
{
await context.Response.OutputStream.WriteAsync( item.Content, 0, item.Content.Length );
}
await context.Response.FlushAsync( );
context.Response.End( );
}
#endregion
#region Helper
private Task GetItemByPathAsync(string path) => service.GetItemByPath( path );
private static bool AssertAuthenticated( HttpContext context )
{
if ( context.User?.Identity == null || !context.User.Identity.IsAuthenticated )
{
context.Response.StatusCode = 403;
context.Response.Write( "Je bent niet ingelogd" );
context.Response.End( );
return false;
}
return true;
}
#endregion
///
/// Invokes an action on the specified .
///
public static async Task DeferredVoidAction( this HttpContext context, [AspMvcAction] string action, Func actionInvoker ) where TController : ControllerBase
{
context.Request.RequestContext.RouteData.Values["controller"] = typeof( TController ).Name.Replace( "Controller", "" );
context.Request.RequestContext.RouteData.Values["action"] = action;
var controller = CreateController( context );
var result = await actionInvoker( controller );
result.ExecuteResult( controller.ControllerContext );
}
///
/// Creates an instance of the given .
///
public static TController CreateController( this HttpContext context ) where TController : ControllerBase
{
var controller = DependencyResolver.Current.GetService( );
controller.ControllerContext = new ControllerContext( context.Request.RequestContext, controller );
return controller;
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... -only-mode