ASP.NET Core DotNetCore 开源GitServer 实现自己的GitHub
ASP.NET Core 2.0 开源Git HTTP Server,实现类似 GitHub、GitLab。
GitHub:https://github.com/linezero/GitServer
设置
"GitSettings": {
"BasePath": "D:\\Git",
"GitPath": "git"
}
需要先安装Git,并确保git 命令可以执行,GitPath 可以是 git 的绝对路径。
目前实现的功能
- 创建仓库
- 浏览仓库
- git客户端push pull
- 数据库支持 SQLite、MSSQL、MySQL
- 支持用户管理仓库
更多功能可以查看readme,也欢迎大家贡献支持。
Git交互
LibGit2Sharp 用于操作Git库,实现创建读取仓库信息及删除仓库。
以下是主要代码:

public Repository CreateRepository(string name)
{
string path = Path.Combine(Settings.BasePath, name);
Repository repo = new Repository(Repository.Init(path, true));
return repo;
} public Repository CreateRepository(string name, string remoteUrl)
{
var path = Path.Combine(Settings.BasePath, name);
try
{
using (var repo = new Repository(Repository.Init(path, true)))
{
repo.Config.Set("core.logallrefupdates", true);
repo.Network.Remotes.Add("origin", remoteUrl, "+refs/*:refs/*");
var logMessage = "";
foreach (var remote in repo.Network.Remotes)
{
IEnumerable<string> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
Commands.Fetch(repo, remote.Name, refSpecs, null, logMessage);
}
return repo;
}
}
catch
{
try
{
Directory.Delete(path, true);
}
catch { }
return null;
}
} public void DeleteRepository(string name)
{
Exception e = null;
for(int i = 0; i < 3; i++)
{
try
{
string path = Path.Combine(Settings.BasePath, name);
Directory.Delete(path, true); }
catch(Exception ex) { e = ex; }
} if (e != null)
throw new GitException("Failed to delete repository", e);
}

执行Git命令
git-upload-pack
git-receive-pack
主要代码 GitCommandResult 实现IActionResult

public async Task ExecuteResultAsync(ActionContext context)
{
HttpResponse response = context.HttpContext.Response;
Stream responseStream = GetOutputStream(context.HttpContext); string contentType = $"application/x-{Options.Service}";
if (Options.AdvertiseRefs)
contentType += "-advertisement"; response.ContentType = contentType; response.Headers.Add("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
response.Headers.Add("Pragma", "no-cache");
response.Headers.Add("Cache-Control", "no-cache, max-age=0, must-revalidate"); ProcessStartInfo info = new ProcessStartInfo(_gitPath, Options.ToString())
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
}; using (Process process = Process.Start(info))
{
GetInputStream(context.HttpContext).CopyTo(process.StandardInput.BaseStream); if (Options.EndStreamWithNull)
process.StandardInput.Write('\0');
process.StandardInput.Dispose(); using (StreamWriter writer = new StreamWriter(responseStream))
{
if (Options.AdvertiseRefs)
{
string service = $"# service={Options.Service}\n";
writer.Write($"{service.Length + 4:x4}{service}0000");
writer.Flush();
} process.StandardOutput.BaseStream.CopyTo(responseStream);
} process.WaitForExit();
}
}

BasicAuthentication 基本认证实现
git http 默认的认证为Basic 基本认证,所以这里实现Basic 基本认证。
在ASP.NET Core 2.0 中 Authentication 变化很大之前1.0的一些代码是无法使用。
首先实现 AuthenticationHandler,然后实现 AuthenticationSchemeOptions,创建 BasicAuthenticationOptions。
最主要就是这两个类,下面两个类为辅助类,用于配置和中间件注册。
更多可以查看官方文档
身份验证
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/
https://docs.microsoft.com/zh-cn/aspnet/core/migration/1x-to-2x/identity-2x

1 public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>
2 {
3 public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
4 : base(options, logger, encoder, clock)
5 { }
6 protected async override Task<AuthenticateResult> HandleAuthenticateAsync()
7 {
8 if (!Request.Headers.ContainsKey("Authorization"))
9 return AuthenticateResult.NoResult();
10
11 string authHeader = Request.Headers["Authorization"];
12 if (!authHeader.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
13 return AuthenticateResult.NoResult();
14
15 string token = authHeader.Substring("Basic ".Length).Trim();
16 string credentialString = Encoding.UTF8.GetString(Convert.FromBase64String(token));
17 string[] credentials = credentialString.Split(':');
18
19 if (credentials.Length != 2)
20 return AuthenticateResult.Fail("More than two strings seperated by colons found");
21
22 ClaimsPrincipal principal = await Options.SignInAsync(credentials[0], credentials[1]);
23
24 if (principal != null)
25 {
26 AuthenticationTicket ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), BasicAuthenticationDefaults.AuthenticationScheme);
27 return AuthenticateResult.Success(ticket);
28 }
29
30 return AuthenticateResult.Fail("Wrong credentials supplied");
31 }
32 protected override Task HandleForbiddenAsync(AuthenticationProperties properties)
33 {
34 Response.StatusCode = 403;
35 return base.HandleForbiddenAsync(properties);
36 }
37
38 protected override Task HandleChallengeAsync(AuthenticationProperties properties)
39 {
40 Response.StatusCode = 401;
41 string headerValue = $"{BasicAuthenticationDefaults.AuthenticationScheme} realm=\"{Options.Realm}\"";
42 Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.WWWAuthenticate, headerValue);
43 return base.HandleChallengeAsync(properties);
44 }
45 }
46
47 public class BasicAuthenticationOptions : AuthenticationSchemeOptions, IOptions<BasicAuthenticationOptions>
48 {
49 private string _realm;
50
51 public IServiceCollection ServiceCollection { get; set; }
52 public BasicAuthenticationOptions Value => this;
53 public string Realm
54 {
55 get { return _realm; }
56 set
57 {
58 _realm = value;
59 }
60 }
61
62 public async Task<ClaimsPrincipal> SignInAsync(string userName, string password)
63 {
64 using (var serviceScope = ServiceCollection.BuildServiceProvider().CreateScope())
65 {
66 var _user = serviceScope.ServiceProvider.GetService<IRepository<User>>();
67 var user = _user.List(r => r.Name == userName && r.Password == password).FirstOrDefault();
68 if (user == null)
69 return null;
70 var identity = new ClaimsIdentity(BasicAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
71 identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
72 var principal = new ClaimsPrincipal(identity);
73 return principal;
74 }
75 }
76 }
77
78 public static class BasicAuthenticationDefaults
79 {
80 public const string AuthenticationScheme = "Basic";
81 }
82 public static class BasicAuthenticationExtensions
83 {
84 public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder)
85 => builder.AddBasic(BasicAuthenticationDefaults.AuthenticationScheme, _ => { _.ServiceCollection = builder.Services;_.Realm = "GitServer"; });
86
87 public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder, Action<BasicAuthenticationOptions> configureOptions)
88 => builder.AddBasic(BasicAuthenticationDefaults.AuthenticationScheme, configureOptions);
89
90 public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder, string authenticationScheme, Action<BasicAuthenticationOptions> configureOptions)
91 => builder.AddBasic(authenticationScheme, displayName: null, configureOptions: configureOptions);
92
93 public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<BasicAuthenticationOptions> configureOptions)
94 {
95 builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IOptions<BasicAuthenticationOptions>, BasicAuthenticationOptions>());
96 return builder.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
97 }
98 }

CookieAuthentication Cookie认证
实现自定义登录,无需identity ,实现注册登录。
主要代码:
启用Cookie
https://github.com/linezero/GitServer/blob/master/GitServer/Startup.cs#L60

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options=> {
options.AccessDeniedPath = "/User/Login";
options.LoginPath = "/User/Login";
})

登录
https://github.com/linezero/GitServer/blob/master/GitServer/Controllers/UserController.cs#L34
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Name));
identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
官方文档介绍:https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x
部署说明
发布后配置数据库及git目录(可以为绝对地址和命令)、git 仓库目录。

{
"ConnectionStrings": {
"ConnectionType": "Sqlite", //Sqlite,MSSQL,MySQL
"DefaultConnection": "Filename=gitserver.db"
},
"GitSettings": {
"BasePath": "D:\\Git",
"GitPath": "git"
}
}

运行后注册账户,登录账户创建仓库,然后根据提示操作,随后git push、git pull 都可以。
ASP.NET Core DotNetCore 开源GitServer 实现自己的GitHub的更多相关文章
- asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程
最近在学习张善友老师的NanoFabric 框架的时了解到Exceptionless : https://exceptionless.com/ !因此学习了一下这个开源框架!下面对Exceptionl ...
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- 【转】asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程
最近在学习张善友老师的NanoFabric 框架的时了解到Exceptionless : https://exceptionless.com/ !因此学习了一下这个开源框架!下面对Exceptionl ...
- ASP.NET Core 开源GitServer 实现自己的GitHub
ASP.NET Core 2.0 开源Git HTTP Server,实现类似 GitHub.GitLab. GitHub:https://github.com/linezero/GitServer ...
- 一个遵循CleanArchitecture原则的Asp.net core轻量级开源项目
这是一个基于最新的ASP.net core 5.0创建Razor Page应用程序解决方案模板.遵循Clean Architecture的原则,以最求简洁的代码风格和实现快速开发小型的web业务系统的 ...
- ASP.NET Core 修改开源协议为MIT,.NET全平台 MIT协议开源了
2021年7月23日,.NET开发团队完成了所有的.NET平台的相关框架的MIT协议更改,我们可以通过 https://github.com/dotnet/aspnetcore/issues/1887 ...
- Asp.net Core 入门实战
Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个合集,方便一次性Clone 目录 快速入门 安装 一个最小的应用 项目模板 路由 静态文件 ...
- Asp.net Core 入门实战 2.请求流程
Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone,喜欢的(Star),本系列持续更新,也可以通过我的网站访问 ...
- ASP.NET Core - 开篇
由来 ASP.NET Core 是一个跨平台的高性能开源框架,ASP.NET Core第一次出现在我们眼前是以 ASP.NET vNext 命名的,然后又重新命名为ASP.NET 5,为了表明它并不是 ...
随机推荐
- redux有价值的文档
使用 Redux 管理状态,第 1 部分 https://www.ibm.com/developerworks/cn/web/wa-manage-state-with-redux-p1-david-g ...
- log4j.properties的配置信息
吃了没日志的亏,以前总以为日志没用,以后要重视起来了,很多错误服务器不会显示,但是页面上就是出错,这个时候就要显示日志了. 日志的代码如下,创建日志文件,文件名为log4j.properties,把这 ...
- MySQL InnoDB primary key根节点常驻内存
mysql的InnoDB存储引擎在设计时是将根节点常驻内存的,也就是说查找某一键值的行记录时最多只需要1~3次磁盘I/O操作.
- 下载 python
https://www.python.org/ftp/python/ https://www.cnblogs.com/linxue/p/10097785.html https://blog.csdn. ...
- js限制输入数字能输入小数点,js定义数组,js往数组中添加数据,js将字符型转为数字型,除法结果保留两位小数——js小测:计算比赛得分
一个朋友跟我说要去给某个比赛算分: 规则:去掉最低分最高分求平均分: 最近在学习大数据可视化——图谱,用到js一些东西,所以今天就用js练练 用到知识点: js限制输入数字能输入小数点,js定义数组, ...
- APP用户隐私协议
告知用户 重视每个用户的的隐私,郑重承诺如下: 一.我们所收集的信息以及如何使用: 我们可能通过您的IP地址,地理位置信息,收集一些非个人隐私的统计资料,使我们能够进一步改善APP的服务.例如,当您浏 ...
- Windows 文件过滤驱动经验总结
Windows 文件过滤驱动经验总结作者:sinister 本文转载自驱动开发网 看了 ChuKuangRen 的第二版<文件过滤驱动开发教程>后,颇有感触.我想,交流都是建立在平等的基础 ...
- MORMOT REST文件上传
MORMOT REST文件上传 上传数据格式必须是:MULTIPART/FORM-DATA uses SynCommons 后端处理参照下列代码: 后端处理参照下列代码: procedure TWeb ...
- XML-Signature 语法和签名
一段 XML-signature 的 demo: <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> &l ...
- 微信小程序:用 Promise 解决方案代替回调地狱。 修复 this._invokeMethod is not a function 的问题
/** * 将回调地狱转换为 Promise 形式 * https://blog.csdn.net/SEAYEHIN/article/details/88663740 * raw: wx.downlo ...