- 新增 WindowsCredential 模型和控制器,用于管理 Windows 凭据 - 新增 RemoteAccessToken 模型,支持生成可分享的远程访问链接 - 更新 RemoteDesktopController,添加 Token 生成、验证、撤销等 API - 更新前端 RemoteDesktopModal,支持4种连接方式:快速连接、生成分享链接、手动输入、链接管理 - 新增 WindowsCredentialManager 组件用于管理 Windows 凭据 - 新增 RemoteAccessPage 用于通过 Token 访问远程桌面 - 添加 Vue Router 支持 /remote/:token 路由 - 更新数据库迁移,添加 WindowsCredentials 和 RemoteAccessTokens 表
196 lines
5.9 KiB
C#
196 lines
5.9 KiB
C#
using AmtScanner.Api.Data;
|
|
using AmtScanner.Api.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AmtScanner.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class WindowsCredentialsController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
private readonly ILogger<WindowsCredentialsController> _logger;
|
|
|
|
public WindowsCredentialsController(AppDbContext context, ILogger<WindowsCredentialsController> logger)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有 Windows 凭据
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<WindowsCredentialDto>>> GetAll()
|
|
{
|
|
var credentials = await _context.WindowsCredentials
|
|
.OrderByDescending(c => c.IsDefault)
|
|
.ThenBy(c => c.Name)
|
|
.Select(c => new WindowsCredentialDto
|
|
{
|
|
Id = c.Id,
|
|
Name = c.Name,
|
|
Username = c.Username,
|
|
Domain = c.Domain,
|
|
IsDefault = c.IsDefault,
|
|
Note = c.Note,
|
|
CreatedAt = c.CreatedAt
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(credentials);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建 Windows 凭据
|
|
/// </summary>
|
|
[HttpPost]
|
|
public async Task<ActionResult<WindowsCredentialDto>> Create([FromBody] CreateWindowsCredentialRequest request)
|
|
{
|
|
// 如果设为默认,取消其他默认
|
|
if (request.IsDefault)
|
|
{
|
|
await _context.WindowsCredentials
|
|
.Where(c => c.IsDefault)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(c => c.IsDefault, false));
|
|
}
|
|
|
|
var credential = new WindowsCredential
|
|
{
|
|
Name = request.Name,
|
|
Username = request.Username,
|
|
Password = request.Password, // 实际生产环境应加密
|
|
Domain = request.Domain,
|
|
IsDefault = request.IsDefault,
|
|
Note = request.Note
|
|
};
|
|
|
|
_context.WindowsCredentials.Add(credential);
|
|
await _context.SaveChangesAsync();
|
|
|
|
_logger.LogInformation("Created Windows credential: {Name}", credential.Name);
|
|
|
|
return Ok(new WindowsCredentialDto
|
|
{
|
|
Id = credential.Id,
|
|
Name = credential.Name,
|
|
Username = credential.Username,
|
|
Domain = credential.Domain,
|
|
IsDefault = credential.IsDefault,
|
|
Note = credential.Note,
|
|
CreatedAt = credential.CreatedAt
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新 Windows 凭据
|
|
/// </summary>
|
|
[HttpPut("{id}")]
|
|
public async Task<ActionResult> Update(long id, [FromBody] UpdateWindowsCredentialRequest request)
|
|
{
|
|
var credential = await _context.WindowsCredentials.FindAsync(id);
|
|
if (credential == null)
|
|
{
|
|
return NotFound(new { error = "凭据不存在" });
|
|
}
|
|
|
|
// 如果设为默认,取消其他默认
|
|
if (request.IsDefault && !credential.IsDefault)
|
|
{
|
|
await _context.WindowsCredentials
|
|
.Where(c => c.IsDefault && c.Id != id)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(c => c.IsDefault, false));
|
|
}
|
|
|
|
credential.Name = request.Name;
|
|
credential.Username = request.Username;
|
|
if (!string.IsNullOrEmpty(request.Password))
|
|
{
|
|
credential.Password = request.Password;
|
|
}
|
|
credential.Domain = request.Domain;
|
|
credential.IsDefault = request.IsDefault;
|
|
credential.Note = request.Note;
|
|
credential.UpdatedAt = DateTime.UtcNow;
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(new { success = true });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除 Windows 凭据
|
|
/// </summary>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult> Delete(long id)
|
|
{
|
|
var credential = await _context.WindowsCredentials.FindAsync(id);
|
|
if (credential == null)
|
|
{
|
|
return NotFound(new { error = "凭据不存在" });
|
|
}
|
|
|
|
_context.WindowsCredentials.Remove(credential);
|
|
await _context.SaveChangesAsync();
|
|
|
|
_logger.LogInformation("Deleted Windows credential: {Name}", credential.Name);
|
|
|
|
return Ok(new { success = true });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置默认凭据
|
|
/// </summary>
|
|
[HttpPost("{id}/set-default")]
|
|
public async Task<ActionResult> SetDefault(long id)
|
|
{
|
|
var credential = await _context.WindowsCredentials.FindAsync(id);
|
|
if (credential == null)
|
|
{
|
|
return NotFound(new { error = "凭据不存在" });
|
|
}
|
|
|
|
// 取消其他默认
|
|
await _context.WindowsCredentials
|
|
.Where(c => c.IsDefault)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(c => c.IsDefault, false));
|
|
|
|
credential.IsDefault = true;
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Ok(new { success = true });
|
|
}
|
|
}
|
|
|
|
public class WindowsCredentialDto
|
|
{
|
|
public long Id { get; set; }
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Username { get; set; } = string.Empty;
|
|
public string? Domain { get; set; }
|
|
public bool IsDefault { get; set; }
|
|
public string? Note { get; set; }
|
|
public DateTime CreatedAt { get; set; }
|
|
}
|
|
|
|
public class CreateWindowsCredentialRequest
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Username { get; set; } = string.Empty;
|
|
public string Password { get; set; } = string.Empty;
|
|
public string? Domain { get; set; }
|
|
public bool IsDefault { get; set; }
|
|
public string? Note { get; set; }
|
|
}
|
|
|
|
public class UpdateWindowsCredentialRequest
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Username { get; set; } = string.Empty;
|
|
public string? Password { get; set; }
|
|
public string? Domain { get; set; }
|
|
public bool IsDefault { get; set; }
|
|
public string? Note { get; set; }
|
|
}
|