From 8ae226e9249f2e1458013fdbeb0430da3af32f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E8=8D=A3=E5=9B=BD?= Date: Wed, 19 Jan 2022 11:09:26 +0800 Subject: [PATCH] [fix]Auth --- ...odule.cs => BasicAuthenticationHandler.cs} | 34 ++++++------------- .../BasicAuthenticationOptions.cs | 8 +++++ .../BasicUser.cs | 8 +++++ .../Logs/logs.txt | 13 +++++++ 4 files changed, 39 insertions(+), 24 deletions(-) rename WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/{BasicAuthHttpModule.cs => BasicAuthenticationHandler.cs} (77%) create mode 100644 WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationOptions.cs create mode 100644 WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicUser.cs diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthHttpModule.cs b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationHandler.cs similarity index 77% rename from WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthHttpModule.cs rename to WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationHandler.cs index 5306a27..cf89949 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthHttpModule.cs +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationHandler.cs @@ -1,14 +1,13 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Http.Headers; using System.Security.Claims; using System.Text; using System.Text.Encodings.Web; using System.Threading.Tasks; +using Microsoft.AspNetCore.Authentication; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Win_in.Sfs.Scp.WebApi.XmlHost; namespace ApiBasicAuth.Security { @@ -34,7 +33,7 @@ namespace ApiBasicAuth.Security Response.Headers.Add("WWW-Authenticate", @"Basic realm='Secure Area'"); return AuthenticateResult.Fail("Missing Authorization Header"); } - User user = null; + BasicUser basicUser = null; try { var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); @@ -44,7 +43,7 @@ namespace ApiBasicAuth.Security var password = credentials[1]; if (username.Equals(_basicOptions.Username) && password.Equals(_basicOptions.Password)) { - user = new User { Id = 1, Username = "admin", Birthday = DateTime.Now }; + basicUser = new BasicUser { Id = 1, Username = "admin"}; } } catch @@ -53,12 +52,12 @@ namespace ApiBasicAuth.Security return AuthenticateResult.Fail("Invalid Authorization Header"); } - if (user == null) + if (basicUser == null) return AuthenticateResult.Fail("Invalid Username or Password"); var claims = new[] { - new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), - new Claim(ClaimTypes.Name, user.Username), + new Claim(ClaimTypes.NameIdentifier, basicUser.Id.ToString()), + new Claim(ClaimTypes.Name, basicUser.Username), }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); @@ -67,17 +66,4 @@ namespace ApiBasicAuth.Security return AuthenticateResult.Success(ticket); } } - - public class BasicAuthenticationOptions - { - public string Username { get; set; } = "admin"; - public string Password { get; set; } = "3edc$RFV"; - } - - public class User - { - public int Id { get; set; } - public string Username { get; set; } - public DateTime Birthday { get; set; } - } -} +} \ No newline at end of file diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationOptions.cs b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationOptions.cs new file mode 100644 index 0000000..3ac57cd --- /dev/null +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationOptions.cs @@ -0,0 +1,8 @@ +namespace ApiBasicAuth.Security +{ + public class BasicAuthenticationOptions + { + public string Username { get; set; } = "admin"; + public string Password { get; set; } = "3edc$RFV"; + } +} \ No newline at end of file diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicUser.cs b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicUser.cs new file mode 100644 index 0000000..07dca66 --- /dev/null +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicUser.cs @@ -0,0 +1,8 @@ +namespace Win_in.Sfs.Scp.WebApi.XmlHost +{ + public class BasicUser + { + public int Id { get; set; } + public string Username { get; set; } + } +} diff --git a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Logs/logs.txt b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Logs/logs.txt index ba0c7ae..1f8db5d 100644 --- a/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Logs/logs.txt +++ b/WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Logs/logs.txt @@ -5715,3 +5715,16 @@ System.Net.Http.HttpRequestException: Response status code does not indicate suc 2022-01-14 16:25:16.424 +08:00 [INF] Executed action Win_in.Sfs.Scp.WebApi.XmlHost.Controllers.TestController.BodyParamTestAsync (Win_in.Sfs.Scp.WebApi.Xml.Host) in 6146.2529ms 2022-01-14 16:25:16.424 +08:00 [INF] Executed endpoint 'Win_in.Sfs.Scp.WebApi.XmlHost.Controllers.TestController.BodyParamTestAsync (Win_in.Sfs.Scp.WebApi.Xml.Host)' 2022-01-14 16:25:16.424 +08:00 [INF] Request finished HTTP/2 POST https://localhost:9977/test/body?name=a&author=ss application/xml 110 - 200 - application/json;+charset=utf-8 6147.1488ms +2022-01-19 10:50:48.421 +08:00 [INF] Starting Win_in.Sfs.Scp.WebApi.Xml.Host. +2022-01-19 10:50:49.546 +08:00 [INF] User profile is available. Using 'C:\Users\changketec\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest. +2022-01-19 10:50:50.399 +08:00 [INF] Now listening on: https://localhost:9977 +2022-01-19 10:50:50.399 +08:00 [INF] Application started. Press Ctrl+C to shut down. +2022-01-19 10:50:50.399 +08:00 [INF] Hosting environment: Development +2022-01-19 10:50:50.399 +08:00 [INF] Content root path: D:\Work\SFS\Backend\Scp\WebApiService\src\Win_in.Sfs.Scp.WebApi.Xml.Host +2022-01-19 10:50:54.804 +08:00 [INF] Request starting HTTP/1.1 GET https://localhost:9977/swagger/index.html - - +2022-01-19 10:50:55.324 +08:00 [INF] Request finished HTTP/1.1 GET https://localhost:9977/swagger/index.html - - - 200 - text/html;charset=utf-8 523.7416ms +2022-01-19 10:50:55.399 +08:00 [INF] Request starting HTTP/1.1 GET https://localhost:9977/_framework/aspnetcore-browser-refresh.js - - +2022-01-19 10:50:55.405 +08:00 [INF] Request finished HTTP/1.1 GET https://localhost:9977/_framework/aspnetcore-browser-refresh.js - - - 200 11963 application/javascript;+charset=utf-8 4.2004ms +2022-01-19 10:50:57.913 +08:00 [INF] Request starting HTTP/1.1 GET https://localhost:9977/swagger/v1/swagger.json - - +2022-01-19 10:50:58.330 +08:00 [INF] Request finished HTTP/1.1 GET https://localhost:9977/swagger/v1/swagger.json - - - 200 - application/json;charset=utf-8 416.9399ms +2022-01-19 11:07:20.964 +08:00 [INF] Application is shutting down...