增加登录过期时间支持
This commit is contained in:
parent
956e3349fe
commit
785457a5f4
|
@ -1,17 +1,19 @@
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using SqlSugar;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Falcon.SugarApi.FalconClaim
|
namespace Falcon.SugarApi.FalconClaim
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 自定义验证方式
|
/// 自定义验证方式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class FalconAuthenticationHandler:IAuthenticationHandler
|
public class FalconAuthenticationHandler : IAuthenticationHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造自定义身份验证方式
|
/// 构造自定义身份验证方式
|
||||||
|
@ -35,23 +37,24 @@ namespace Falcon.SugarApi.FalconClaim
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<AuthenticateResult> AuthenticateAsync() {
|
public Task<AuthenticateResult> AuthenticateAsync() {
|
||||||
if(!this.Context.Request.Headers.TryGetValue(FalconClaimOption.FalconAuthenticationKey,out var val)) {
|
if (!this.Context.Request.Headers.TryGetValue(FalconClaimOption.FalconAuthenticationKey, out var val)) {
|
||||||
return UnLoginResultTask;
|
return UnLoginResultTask;
|
||||||
}
|
}
|
||||||
var token = val.ToString();
|
var token = val.ToString();
|
||||||
if(token.IsNullOrEmpty()) {
|
if (token.IsNullOrEmpty()) {
|
||||||
return UnLoginResultTask;
|
return UnLoginResultTask;
|
||||||
}
|
}
|
||||||
if(FalconClaimOption.TokenPrefix.IsNotNullOrEmpty() && !token.StartsWith(FalconClaimOption.TokenPrefix)) {
|
if (FalconClaimOption.TokenPrefix.IsNotNullOrEmpty() && !token.StartsWith(FalconClaimOption.TokenPrefix)) {
|
||||||
return UnLoginResultTask;
|
return UnLoginResultTask;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
var ticket = GetTicket(token);
|
var ticket = GetTicket(token);
|
||||||
if(ticket == null) {
|
if (ticket == null) {
|
||||||
return UnLoginResultTask;
|
return UnLoginResultTask;
|
||||||
}
|
}
|
||||||
return Task.FromResult(AuthenticateResult.Success(ticket));
|
return Task.FromResult(AuthenticateResult.Success(ticket));
|
||||||
} catch(Exception) {
|
}
|
||||||
|
catch (Exception) {
|
||||||
return UnLoginResultTask;
|
return UnLoginResultTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +72,7 @@ namespace Falcon.SugarApi.FalconClaim
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task InitializeAsync(AuthenticationScheme scheme,HttpContext context) {
|
public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) {
|
||||||
this.Scheme = scheme; this.Context = context;
|
this.Scheme = scheme; this.Context = context;
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
@ -82,16 +85,22 @@ namespace Falcon.SugarApi.FalconClaim
|
||||||
List<Claim>? claims = null;
|
List<Claim>? claims = null;
|
||||||
try {
|
try {
|
||||||
claims = this.TokenBuilter.GetClaims(token);
|
claims = this.TokenBuilter.GetClaims(token);
|
||||||
} catch(Exception) {
|
}
|
||||||
|
catch (Exception) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if(claims == null || claims.Count == 0) {
|
if (claims == null || claims.Count == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//检测是否设置过期时间
|
||||||
|
var dtExp = claims.Where(m => m.Type == ClaimTypes.Expiration);
|
||||||
|
if (dtExp.Any() && !DateTime.TryParse(dtExp.First().Value, out var et) && et > DateTime.Now) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var cid = new ClaimsIdentity(FalconClaimOption.SchemeName);
|
var cid = new ClaimsIdentity(FalconClaimOption.SchemeName);
|
||||||
cid.AddClaims(claims);
|
cid.AddClaims(claims);
|
||||||
var principal = new ClaimsPrincipal(cid);
|
var principal = new ClaimsPrincipal(cid);
|
||||||
return new AuthenticationTicket(principal,this.Scheme.Name);
|
return new AuthenticationTicket(principal, this.Scheme.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -14,5 +14,9 @@
|
||||||
/// 密码
|
/// 密码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? Password { get; set; }
|
public string? Password { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 过期小时数。0或者空为不限制
|
||||||
|
/// </summary>
|
||||||
|
public int? ExpHours { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user