完成用户凭据生成相关方法,登录相关接口实现

This commit is contained in:
falcon 2020-04-08 16:28:42 +08:00
parent a3556cf246
commit 4e55b1938d
9 changed files with 163 additions and 8 deletions

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using FAuth.Extensions.Decryptor;
namespace FAuth.Controllers.api
{
@ -15,7 +16,16 @@ namespace FAuth.Controllers.api
/// </summary>
public class UserController:ApiControllerBase<UserController>
{
public UserController(ILogger<UserController> logger,IServiceProvider service) : base(logger,service) {
public IUserTicketDryptor UserTicketDryptor { get; set; }
public UserController(ILogger<UserController> logger,IServiceProvider service,IUserTicketDryptor userTicketDryptor)
: base(logger,service) {
if(logger is null)
throw new ArgumentNullException(nameof(logger));
if(service is null)
throw new ArgumentNullException(nameof(service));
this.UserTicketDryptor = userTicketDryptor ?? throw new ArgumentNullException(nameof(userTicketDryptor));
}
/// <summary>
@ -29,7 +39,9 @@ namespace FAuth.Controllers.api
public CheckUserResult CheckUser(string userName,string password) {
return new CheckUserResult {
Result = userName == password,
Ticket = Guid.NewGuid().ToString(),
Ticket = this.UserTicketDryptor.Encrypt(new UserTicketModel {
UserName = userName,
}),
};
}
@ -41,8 +53,9 @@ namespace FAuth.Controllers.api
[HttpPost]
[ProducesResponseType(typeof(UserInfo),200)]
public UserInfo GetUserByTicket([BindRequired]string ticket) {
var userTicketModel = this.UserTicketDryptor.Decrypt(ticket);
return new UserInfo {
UserName = "aaa"
UserName = userTicketModel.UserName,
};
}

View File

@ -0,0 +1,22 @@
namespace FAuth.Extensions.Decryptor
{
/// <summary>
/// 用户票据加密提供器
/// </summary>
public interface IUserTicketDryptor
{
/// <summary>
/// 通过票据获取用户信息
/// </summary>
/// <param name="content">用户票据</param>
/// <returns>用户信息</returns>
public UserTicketModel Decrypt(string content);
/// <summary>
/// 通过用户信息生成用户票据
/// </summary>
/// <param name="user">用户信息</param>
/// <returns>用户票据</returns>
public string Encrypt(UserTicketModel user);
}
}

View File

@ -0,0 +1,35 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FAuth.Extensions.Decryptor
{
/// <summary>
/// 扩展IServiceCollection注册IUserTicketDryptor
/// </summary>
public static class ServiceCollentionExtent
{
/// <summary>
/// 通过显示配置注册IUserTicketDryptor
/// </summary>
/// <param name="service">服务集合</param>
/// <param name="action">配置选项的方法</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddUserTicketDryptor(this IServiceCollection service,Action<UserTicketDecryptorOption> action) {
service.Configure<UserTicketDecryptorOption>(action);
service.AddSingleton<IUserTicketDryptor,UserTicketDryptor>();
return service;
}
/// <summary>
/// 通过提供配置节点信息注册IUserTicketDryptor
/// </summary>
/// <param name="services">服务集合</param>
/// <param name="section">配置文件节点</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddUserTicketDryptor(this IServiceCollection services,IConfigurationSection section) {
services.Configure<UserTicketDecryptorOption>(section);
services.AddSingleton<IUserTicketDryptor,UserTicketDryptor>();
return services;
}
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.Extensions.Options;
namespace FAuth.Extensions.Decryptor
{
/// <summary>
/// 用户加密配置
/// </summary>
public class UserTicketDecryptorOption:IOptions<UserTicketDecryptorOption>
{
public UserTicketDecryptorOption Value => this;
/// <summary>
/// 加密用key
/// </summary>
public string Key { get; set; }
}
}

View File

@ -0,0 +1,52 @@
using System;
using Falcon.Extend;
using Microsoft.Extensions.Options;
namespace FAuth.Extensions.Decryptor
{
/// <summary>
/// 用户凭据实现
/// </summary>
public class UserTicketDryptor:IUserTicketDryptor
{
/// <summary>
/// 基础加密解密服务
/// </summary>
public ICryptoService CryptoService { get; set; }
/// <summary>
/// 用户凭据配置
/// </summary>
public UserTicketDecryptorOption Option { get; set; }
/// <summary>
/// 基础Json序列化提供器
/// </summary>
public IJsonProvider JsonProvider { get; set; }
/// <summary>
/// 构造用户凭据实现
/// </summary>
/// <param name="s">基础加密解密提供器</param>
/// <param name="option">凭据选项</param>
/// <param name="json">Json序列化器</param>
public UserTicketDryptor(ICryptoService s,IOptions<UserTicketDecryptorOption> option,IJsonProvider json) {
this.CryptoService = s ?? throw new ArgumentNullException(nameof(s));
this.Option = option?.Value ?? throw new ArgumentNullException(nameof(option));
this.JsonProvider = json ?? throw new ArgumentNullException(nameof(json));
}
/// <summary>
/// 通过用户凭据获得用户信息
/// </summary>
/// <param name="content">用户凭据</param>
/// <returns>用户信息</returns>
public UserTicketModel Decrypt(string content) =>
this.JsonProvider.GetObj<UserTicketModel>(this.CryptoService.Decrypt(content,this.Option.Key));
/// <summary>
/// 通过用户信息生成用户凭据
/// </summary>
/// <param name="user">用户信息</param>
/// <returns>用户凭据</returns>
public string Encrypt(UserTicketModel user) =>
this.CryptoService.Encrypt(this.JsonProvider.GetJson(user),Option.Key);
}
}

View File

@ -0,0 +1,13 @@
namespace FAuth.Extensions.Decryptor
{
/// <summary>
/// 用户票据中的用户信息
/// </summary>
public class UserTicketModel
{
/// <summary>
/// 登录用户名称
/// </summary>
public string UserName { get; set; }
}
}

View File

@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Falcon.Extend" Version="1.1.1" />
<PackageReference Include="Falcon.Extend" Version="1.2.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
<PackageReference Include="NLog" Version="4.7.0" />
@ -20,8 +20,4 @@
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>
<ItemGroup>
<Folder Include="Extensions\Decryptor\" />
</ItemGroup>
</Project>

View File

@ -4,6 +4,7 @@ using System.Text.Encodings.Web;
using System.Text.Unicode;
using Falcon.Extend;
using FAuth.DataBase;
using FAuth.Extensions.Decryptor;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Redis;
@ -59,6 +60,10 @@ namespace FAuth
c.IncludeXmlComments(xmlPath,true);
c.AddXmlEnumEnable(xmlPath);
});
//注册用户票据生成器
services.AddAESCrypto();
var UTDO = Configuration.GetSection("UserTicketDecryptorOption");
services.AddUserTicketDryptor(UTDO);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -13,5 +13,8 @@
"Redis": {
"InstanceName": "",
"Configuration": "127.0.0.1:7001,password=123654"
},
"UserTicketDecryptorOption": {
"Key": "abcd"
}
}