@ -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 u ser = null ;
BasicUser basicU ser = 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 ) )
{
u ser = new User { Id = 1 , Username = "admin" , Birthday = DateTime . Now } ;
basicU ser = new Basic User { Id = 1 , Username = "admin" } ;
}
}
catch
@ -53,12 +52,12 @@ namespace ApiBasicAuth.Security
return AuthenticateResult . Fail ( "Invalid Authorization Header" ) ;
}
if ( u ser = = null )
if ( basicU ser = = null )
return AuthenticateResult . Fail ( "Invalid Username or Password" ) ;
var claims = new [ ] {
new Claim ( ClaimTypes . NameIdentifier , u ser. Id . ToString ( ) ) ,
new Claim ( ClaimTypes . Name , u ser. Username ) ,
new Claim ( ClaimTypes . NameIdentifier , basicU ser. Id . ToString ( ) ) ,
new Claim ( ClaimTypes . Name , basicU ser. 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 ; }
}
}
}