Browse Source

[fix]Auth

master
贾荣国 3 years ago
parent
commit
8ae226e924
  1. 32
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationHandler.cs
  2. 8
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthenticationOptions.cs
  3. 8
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicUser.cs
  4. 13
      WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/Logs/logs.txt

32
WebApiService/src/Win_in.Sfs.Scp.WebApi.Xml.Host/BasicAuthHttpModule.cs → 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;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; 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 namespace ApiBasicAuth.Security
{ {
@ -34,7 +33,7 @@ namespace ApiBasicAuth.Security
Response.Headers.Add("WWW-Authenticate", @"Basic realm='Secure Area'"); Response.Headers.Add("WWW-Authenticate", @"Basic realm='Secure Area'");
return AuthenticateResult.Fail("Missing Authorization Header"); return AuthenticateResult.Fail("Missing Authorization Header");
} }
User user = null; BasicUser basicUser = null;
try try
{ {
var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
@ -44,7 +43,7 @@ namespace ApiBasicAuth.Security
var password = credentials[1]; var password = credentials[1];
if (username.Equals(_basicOptions.Username) && password.Equals(_basicOptions.Password)) 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 catch
@ -53,12 +52,12 @@ namespace ApiBasicAuth.Security
return AuthenticateResult.Fail("Invalid Authorization Header"); return AuthenticateResult.Fail("Invalid Authorization Header");
} }
if (user == null) if (basicUser == null)
return AuthenticateResult.Fail("Invalid Username or Password"); return AuthenticateResult.Fail("Invalid Username or Password");
var claims = new[] { var claims = new[] {
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.NameIdentifier, basicUser.Id.ToString()),
new Claim(ClaimTypes.Name, user.Username), new Claim(ClaimTypes.Name, basicUser.Username),
}; };
var identity = new ClaimsIdentity(claims, Scheme.Name); var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity); var principal = new ClaimsPrincipal(identity);
@ -67,17 +66,4 @@ namespace ApiBasicAuth.Security
return AuthenticateResult.Success(ticket); 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; }
}
} }

8
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";
}
}

8
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; }
}
}

13
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 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] 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-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...

Loading…
Cancel
Save