Pārlūkot izejas kodu

Internalizing types that were intended to only be used internally in IIS (#27441)

Justin Kotalik 5 gadi atpakaļ
vecāks
revīzija
24eb03519e

+ 1 - 1
src/Servers/IIS/IIS/src/Core/HttpResponseStream.cs

@@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Http.Features;
 
 namespace Microsoft.AspNetCore.Server.IIS.Core
 {
-     internal class HttpResponseStream : WriteOnlyStream
+     internal class HttpResponseStream : WriteOnlyStreamInternal
      {
         private readonly IHttpBodyControlFeature _bodyControl;
         private readonly IISHttpContext _context;

+ 1 - 1
src/Servers/IIS/IIS/src/Core/IISHttpServer.cs

@@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
 
             if (_options.ForwardWindowsAuthentication)
             {
-                authentication.AddScheme(new AuthenticationScheme(IISServerDefaults.AuthenticationScheme, _options.AuthenticationDisplayName, typeof(IISServerAuthenticationHandler)));
+                authentication.AddScheme(new AuthenticationScheme(IISServerDefaults.AuthenticationScheme, _options.AuthenticationDisplayName, typeof(IISServerAuthenticationHandlerInternal)));
             }
 
             Features.Set<IServerAddressesFeature>(_serverAddressesFeature);

+ 1 - 0
src/Servers/IIS/IIS/src/Core/IISServerAuthenticationHandler.cs

@@ -11,6 +11,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
     /// <summary>
     /// The default authentication handler with IIS In-Process
     /// </summary>
+    [Obsolete("The IISServerAuthenticationHandler is obsolete and will be removed in a future release.")]
     public class IISServerAuthenticationHandler : IAuthenticationHandler
     {
         private HttpContext _context;

+ 65 - 0
src/Servers/IIS/IIS/src/Core/IISServerAuthenticationHandlerInternal.cs

@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Authentication;
+using Microsoft.AspNetCore.Http;
+
+namespace Microsoft.AspNetCore.Server.IIS.Core
+{
+    /// <summary>
+    /// The default authentication handler with IIS In-Process
+    /// </summary>
+    internal class IISServerAuthenticationHandlerInternal : IAuthenticationHandler
+    {
+        private HttpContext _context;
+        private IISHttpContext _iisHttpContext;
+
+        internal AuthenticationScheme Scheme { get; private set; }
+
+        ///<inheritdoc/>
+        public Task<AuthenticateResult> AuthenticateAsync()
+        {
+            var user = _iisHttpContext.WindowsUser;
+            if (user != null && user.Identity != null && user.Identity.IsAuthenticated)
+            {
+                return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(user, Scheme.Name)));
+            }
+            else
+            {
+                return Task.FromResult(AuthenticateResult.NoResult());
+            }
+        }
+
+        ///<inheritdoc/>
+        public Task ChallengeAsync(AuthenticationProperties properties)
+        {
+            // We would normally set the www-authenticate header here, but IIS does that for us.
+            _context.Response.StatusCode = 401;
+            return Task.CompletedTask;
+        }
+
+        ///<inheritdoc/>
+        public Task ForbidAsync(AuthenticationProperties properties)
+        {
+            _context.Response.StatusCode = 403;
+            return Task.CompletedTask;
+        }
+
+        ///<inheritdoc/>
+        public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
+        {
+            _iisHttpContext = context.Features.Get<IISHttpContext>();
+            if (_iisHttpContext == null)
+            {
+                throw new InvalidOperationException("No IISHttpContext found.");
+            }
+
+            Scheme = scheme;
+            _context = context;
+
+            return Task.CompletedTask;
+        }
+    }
+}

+ 2 - 2
src/Servers/IIS/IIS/src/Core/Streams.cs

@@ -9,8 +9,8 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
 {
     internal class Streams
     {
-        private static readonly ThrowingWasUpgradedWriteOnlyStream _throwingResponseStream
-            = new ThrowingWasUpgradedWriteOnlyStream();
+        private static readonly ThrowingWasUpgradedWriteOnlyStreamInternal _throwingResponseStream
+            = new ThrowingWasUpgradedWriteOnlyStreamInternal();
 
         private readonly IISHttpContext _context;
         private readonly HttpResponseStream _response;

+ 1 - 0
src/Servers/IIS/IIS/src/Core/ThrowingWasUpgradedWriteOnlyStream.cs

@@ -14,6 +14,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
     /// <remarks>
     /// Users should not need to instantiate this class.
     /// </remarks>
+    [Obsolete("The ThrowingWasUpgradedWriteOnlyStream is obsolete and will be removed in a future release.")]
     public class ThrowingWasUpgradedWriteOnlyStream : WriteOnlyStream
     {
         ///<inheritdoc/>

+ 39 - 0
src/Servers/IIS/IIS/src/Core/ThrowingWasUpgradedWriteOnlyStreamInternal.cs

@@ -0,0 +1,39 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Microsoft.AspNetCore.Server.IIS.Core
+{
+    /// <summary>
+    /// A <see cref="Stream"/> which throws on calls to write after the stream was upgraded
+    /// </summary>
+    /// <remarks>
+    /// Users should not need to instantiate this class.
+    /// </remarks>
+    internal class ThrowingWasUpgradedWriteOnlyStreamInternal : WriteOnlyStreamInternal
+    {
+        ///<inheritdoc/>
+        public override void Write(byte[] buffer, int offset, int count)
+            => throw new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded);
+
+        ///<inheritdoc/>
+        public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+            => throw new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded);
+
+        ///<inheritdoc/>
+        public override void Flush()
+            => throw new InvalidOperationException(CoreStrings.ResponseStreamWasUpgraded);
+
+        ///<inheritdoc/>
+        public override long Seek(long offset, SeekOrigin origin)
+            => throw new NotSupportedException();
+
+        ///<inheritdoc/>
+        public override void SetLength(long value)
+            => throw new NotSupportedException();
+    }
+}

+ 1 - 0
src/Servers/IIS/IIS/src/Core/WriteOnlyStream.cs

@@ -11,6 +11,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
     /// <summary>
     /// A <see cref="Stream"/> which only allows for writes.
     /// </summary>
+    [Obsolete("The WriteOnlyStream is obsolete and will be removed in a future release.")]
     public abstract class WriteOnlyStream : Stream
     {
         ///<inheritdoc/>

+ 66 - 0
src/Servers/IIS/IIS/src/Core/WriteOnlyStreamInternal.cs

@@ -0,0 +1,66 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Microsoft.AspNetCore.Server.IIS.Core
+{
+    /// <summary>
+    /// A <see cref="Stream"/> which only allows for writes.
+    /// </summary>
+    internal abstract class WriteOnlyStreamInternal : Stream
+    {
+        ///<inheritdoc/>
+        public override bool CanRead => false;
+
+        ///<inheritdoc/>
+        public override bool CanWrite => true;
+
+        ///<inheritdoc/>
+        public override int ReadTimeout
+        {
+            get => throw new NotSupportedException();
+            set => throw new NotSupportedException();
+        }
+
+        ///<inheritdoc/>
+        public override bool CanSeek => false;
+
+        ///<inheritdoc/>
+        public override long Length => throw new NotSupportedException();
+
+        ///<inheritdoc/>
+        public override long Position
+        {
+            get => throw new NotSupportedException();
+            set => throw new NotSupportedException();
+        }
+
+        ///<inheritdoc/>
+        public override int Read(byte[] buffer, int offset, int count)
+        {
+            throw new NotSupportedException();
+        }
+
+        ///<inheritdoc/>
+        public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+        {
+            throw new NotSupportedException();
+        }
+
+        ///<inheritdoc/>
+        public override long Seek(long offset, SeekOrigin origin)
+        {
+            throw new NotSupportedException();
+        }
+
+        ///<inheritdoc/>
+        public override void SetLength(long value)
+        {
+            throw new NotSupportedException();
+        }
+    }
+}