Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#增加前端测试#
  • Loading branch information
liaozb committed Jul 25, 2018
commit 5d0d793bf87d18d28b5840cc3ae2db013b2e3866
12 changes: 12 additions & 0 deletions APIJSON.NET/APIJSON.NET.Test/APIJSON.NET.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RestSharp" Version="106.3.1" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions APIJSON.NET/APIJSON.NET.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using RestSharp;
using System;

namespace APIJSON.NET.Test
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("http://localhost:5000/");
var request = new RestRequest("get", Method.POST);
request.AddJsonBody(@"{
'User': {
'id': 38710
}
}
");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

request = new RestRequest("get", Method.POST);
request.AddJsonBody(@"{
'User': {
'id': 38710
},
'[]': {
'page': 0,
'count': 3,
'Moment': {
'userId': 38710
},
'Comment[]': {
'count': 3,
'Comment': {
'momentId@': '[]/Moment/id'
}
}
}
}
");
IRestResponse response2 = client.Execute(request);
Console.WriteLine(response2.Content);


Console.ReadLine();
}
}
}
6 changes: 6 additions & 0 deletions APIJSON.NET/APIJSON.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27703.2035
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIJSON.NET", "APIJSON.NET\APIJSON.NET.csproj", "{FF647576-A104-4D54-954D-3547B4FDCDB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIJSON.NET.Test", "APIJSON.NET.Test\APIJSON.NET.Test.csproj", "{0828346E-207E-49F8-AD57-E1AB6B6E4077}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{FF647576-A104-4D54-954D-3547B4FDCDB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF647576-A104-4D54-954D-3547B4FDCDB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF647576-A104-4D54-954D-3547B4FDCDB2}.Release|Any CPU.Build.0 = Release|Any CPU
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0828346E-207E-49F8-AD57-E1AB6B6E4077}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 0 additions & 4 deletions APIJSON.NET/APIJSON.NET/APIJSON.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.0" />
Expand Down
2 changes: 1 addition & 1 deletion APIJSON.NET/APIJSON.NET/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class HomeController : Controller
{
public IActionResult Index()
{
return Redirect("/swagger");
return Redirect("index.html");
}
}
}
8 changes: 4 additions & 4 deletions APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public ActionResult Query([FromBody]string json)
}
else
{
var ddf = selectTable.GetTableData(subtable, 0, 0, where[i].ToString(), zht);
if (ddf.Item1 != null)
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
if (ddf != null)
{
zht.Add(subtable, JToken.FromObject(ddf.Item1));
zht.Add(subtable, JToken.FromObject(ddf));
}
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public ActionResult Query([FromBody]string json)
}
else if (key.IsTable())
{
var template = selectTable.GetTableData(key, 0, 0, item.Value.ToString(), ht).Item1;
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
if (template != null)
{
ht.Add(key, JToken.FromObject(template));
Expand Down
38 changes: 26 additions & 12 deletions APIJSON.NET/APIJSON.NET/Controllers/TokenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -16,29 +17,37 @@ namespace APIJSON.NET.Controllers
[ApiController]
public class TokenController : ControllerBase
{
private DbOptions _options;
private DbContext db;
private readonly IOptions<TokenAuthConfiguration> _configuration;
public TokenController(IOptions<DbOptions> options, IOptions<TokenAuthConfiguration> configuration)
public TokenController(DbContext _db, IOptions<TokenAuthConfiguration> configuration)
{
this._options = options.Value;
_configuration = configuration;
db = _db;
}
[HttpPost("/token")]
public IActionResult Create(string username, string password)
[HttpGet("/token")]
public IActionResult Create(TokenInput input)
{
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
if (username!=password)
var us = db.LoginDb.GetSingle(it => it.userName == input.username);
if (us==null)
{

ht["code"] = "201";
ht["msg"] = "用户名或者密码错误!";
return Ok(ht);
}
string str = SimpleStringCipher.Instance.Encrypt(input.password,null, Encoding.ASCII.GetBytes(us.passWordSalt));
if (!us.passWord.Equals(str))
{
ht["code"]="201";
ht["msg"]= "用户名或者密码错误!";
return Ok(ht);
}

var identity = new ClaimsIdentity();
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "1"));
identity.AddClaim(new Claim(ClaimTypes.Name, "1"));
identity.AddClaim(new Claim(ClaimTypes.Role, ""));
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Sub, username));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, us.userId.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Role, us.roleCode));
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Sub, input.username));
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
identity.AddClaim(new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.Now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64));

Expand Down Expand Up @@ -69,6 +78,11 @@ private string CreateAccessToken(IEnumerable<Claim> claims, TimeSpan? expiration
return new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
}
}
public class TokenInput
{
public string username { get; set; }
public string password { get; set; }
}
public class AuthenticateResultModel
{
public string AccessToken { get; set; }
Expand Down
28 changes: 23 additions & 5 deletions APIJSON.NET/APIJSON.NET/Data/DbContext.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
using Microsoft.Extensions.Options;
using APIJSON.NET.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using SqlSugar;
using System;
using System.Collections.Generic;

namespace APIJSON.NET
{
public class DbContext
{
public DbContext(IOptions<DbOptions> options)
public DbContext(IConfiguration options)
{
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = options.Value.ConnectionString,
DbType = options.Value.DbType,
IsAutoCloseConnection = true
ConnectionString = options.GetConnectionString("ConnectionString"),
DbType = (DbType)Enum.Parse(typeof(SqlSugar.DbType), options.GetConnectionString("DbType")),
IsAutoCloseConnection = true,
InitKeyType= InitKeyType.Attribute
});
}
public SqlSugarClient Db;
public DbSet<Login> LoginDb { get { return new DbSet<Login>(Db); } }
}
public class DbSet<T> : SimpleClient<T> where T : class, new()
{
public DbSet(SqlSugarClient context) : base(context)
{

}
public List<T> GetByIds(dynamic[] ids)
{
return Context.Queryable<T>().In(ids).ToList(); ;
}
}

}
39 changes: 39 additions & 0 deletions APIJSON.NET/APIJSON.NET/Data/DbInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using APIJSON.NET.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
namespace APIJSON.NET
{
public static class DbInit
{
public static void Initialize(IApplicationBuilder app)
{
var db = app.ApplicationServices.GetRequiredService<DbContext>();

db.Db.CodeFirst.InitTables(typeof(Login));
if (!db.LoginDb.IsAny(it=>it.userId>0))
{
var ds = new List<Login>();

for (int i = 1; i < 10; i++)
{
var d = new Login();
d.userId = i;
d.userName = "admin"+i.ToString();
d.passWordSalt = Guid.NewGuid().ToString();
d.passWord = SimpleStringCipher.Instance.Encrypt("123456", null, Encoding.ASCII.GetBytes(d.passWordSalt));
d.roleCode = "role1";
d.CreateTime = DateTime.Now;
ds.Add(d);
}
db.LoginDb.InsertRange(ds.ToArray());


}

}
}
}
Loading