|
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using HttpClientStudy.Model;
|
|
|
|
|
using HttpClientStudy.Service;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
|
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
|
|
|
|
|
namespace HttpClientStudy.WebApp
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// HttpClientѧϰ<D1A7><CFB0>WebAPI<50><49>Ŀ
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Main
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
#region <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
|
|
|
|
|
//Session<6F>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
builder.Services.AddDistributedMemoryCache();
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Session
|
|
|
|
|
builder.Services.AddSession(option =>
|
|
|
|
|
{
|
|
|
|
|
option.Cookie.Name = "HttpClientStudy";
|
|
|
|
|
option.IOTimeout = TimeSpan.FromHours(1);
|
|
|
|
|
option.IdleTimeout = TimeSpan.FromHours(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Kestrel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
|
|
|
|
|
builder.Services.Configure<KestrelServerOptions>( option =>
|
|
|
|
|
{
|
|
|
|
|
//ASP.NET Core 3.0 ֮ǰ<D6AE>İ汾<C4B0><E6B1BE>AllowSynchronousIO Ĭ<><C4AC><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD> true <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC> IO <20><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Get<65><74><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.<2E><>ֻ<EFBFBD><D6BB>ֱ<EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ģ<EFBFBD>Ͱ<CDB0>
|
|
|
|
|
option.AllowSynchronousIO = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Form<72><6D><EFBFBD><EFBFBD><EFBFBD>ύѡ<E1BDBB><D1A1>
|
|
|
|
|
builder.Services.Configure<FormOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
//options.BufferBody = true;
|
|
|
|
|
options.MultipartBodyLengthLimit = long.MaxValue;
|
|
|
|
|
options.MultipartBoundaryLengthLimit = int.MaxValue;
|
|
|
|
|
options.MultipartHeadersCountLimit = int.MaxValue;
|
|
|
|
|
options.MultipartHeadersLengthLimit = int.MaxValue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Swagger
|
|
|
|
|
builder.Services.AddSwaggerGen(setup =>
|
|
|
|
|
{
|
|
|
|
|
#region <20><><EFBFBD><EFBFBD>Swagger<65>ĵ<EFBFBD>
|
|
|
|
|
//name<6D><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪSwaggerUI<55><49>SwaggerEndpoint<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>{documentName}
|
|
|
|
|
//<2F><><EFBFBD>߱<EFBFBD><DFB1>뱣<EFBFBD><EBB1A3>һ<EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>쳣
|
|
|
|
|
setup.SwaggerDoc(name: "v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "HttpClientѧϰ", Version = "<22><>1<EFBFBD><31>" });
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region <20><><EFBFBD><EFBFBD>xmlע<6C><D7A2>
|
|
|
|
|
var xmlCommentFiles = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "HttpClientStudy.*.xml", System.IO.SearchOption.TopDirectoryOnly);
|
|
|
|
|
foreach (var xmlFile in xmlCommentFiles)
|
|
|
|
|
{
|
|
|
|
|
//includeControllerXmlComments<74><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>xmlע<6C><D7A2>
|
|
|
|
|
setup.IncludeXmlComments(filePath: xmlFile, includeControllerXmlComments: true);
|
|
|
|
|
|
|
|
|
|
setup.UseInlineDefinitionsForEnums();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region <20><><EFBFBD>ýӿ<C3BD>Auth<74><68>Ȩ<EFBFBD><C8A8>ť
|
|
|
|
|
setup.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Description = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Bearer<65><72>Token<65><6E>Bearer {Token}",
|
|
|
|
|
|
|
|
|
|
//jwtĬ<74>ϵIJ<CFB5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
|
|
|
|
|
//jwtĬ<74>ϴ<EFBFBD><CFB4><EFBFBD> Authorization <20><>Ϣ<EFBFBD><CFA2>λ<EFBFBD>ã<EFBFBD><C3A3>˴<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
|
|
|
|
|
//<2F><>֤<EFBFBD><D6A4><EFBFBD>ͣ<EFBFBD><CDA3>˴<EFBFBD>ʹ<EFBFBD><CAB9>Api Key
|
|
|
|
|
Type = SecuritySchemeType.ApiKey
|
|
|
|
|
});
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>÷<EFBFBD>Χ
|
|
|
|
|
setup.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
{
|
|
|
|
|
Id = "Bearer",
|
|
|
|
|
Type = ReferenceType.SecurityScheme
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new List<string>()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
|
|
|
|
|
setup.EnableAnnotations();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>CORS<52><53><EFBFBD><EFBFBD>
|
|
|
|
|
builder.Services.AddCors(option =>
|
|
|
|
|
{
|
|
|
|
|
option.AddPolicy("AllowAll", builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//<2F><>֤
|
|
|
|
|
builder.Services //<2F><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD>
|
|
|
|
|
.AddAuthentication(authOption =>
|
|
|
|
|
{
|
|
|
|
|
authOption.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
authOption.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
authOption.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
authOption.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
authOption.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
})
|
|
|
|
|
//Cookie<69><65>֤
|
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, option =>
|
|
|
|
|
{
|
|
|
|
|
option.Cookie.Name = ".eds.editor.cookie.authentication.oa2";//<2F><><EFBFBD>ô洢<C3B4>û<EFBFBD><C3BB><EFBFBD>¼<EFBFBD><C2BC>Ϣ<EFBFBD><CFA2><EFBFBD>û<EFBFBD>Token<65><6E>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>Cookie<69><65><EFBFBD><EFBFBD>
|
|
|
|
|
option.Cookie.HttpOnly = true;//<2F><><EFBFBD>ô洢<C3B4>û<EFBFBD><C3BB><EFBFBD>¼<EFBFBD><C2BC>Ϣ<EFBFBD><CFA2><EFBFBD>û<EFBFBD>Token<65><6E>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>Cookie<69><65><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD>(<28><>JavaScript<70><74>)<29><><EFBFBD>ʵ<EFBFBD>
|
|
|
|
|
option.ExpireTimeSpan = TimeSpan.FromDays(3);// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
option.SlidingExpiration = true;// <20>Ƿ<EFBFBD><C7B7>ڹ<EFBFBD><DAB9><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
option.LoginPath = "/Account/Login";
|
|
|
|
|
option.LogoutPath = "/Account/LoginOut";
|
|
|
|
|
//option.AccessDeniedPath = "/Account/Login";
|
|
|
|
|
})
|
|
|
|
|
//<2F><>֤
|
|
|
|
|
.AddJwtBearer(option =>
|
|
|
|
|
{
|
|
|
|
|
option.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidIssuer = "WWW.WANGGAOFENG.CN",
|
|
|
|
|
ValidAudience = "WWW.WANGGAOFENG.CN",
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("0123456789abcdefghigklmnopqrstdf41sadfweqtdfghsdfgsdfweqr")),
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䣬<EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD>Чʱ<D0A7><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>jwt<77>Ĺ<EFBFBD><C4B9><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
ClockSkew = TimeSpan.FromSeconds(0)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//<2F><>Ȩ
|
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
//<2F><>ͨ<EFBFBD><CDA8>
|
|
|
|
|
|
|
|
|
|
builder.Services.AddScoped(provider => new Account() { Id = 0, Name = "<22><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>ʾ<EFBFBD><CABE>", Password = "123456", Role = "IoC" });
|
|
|
|
|
builder.Services.AddScoped<AccountService>();
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
#region <20><><EFBFBD><EFBFBD>Http<74>ܵ<EFBFBD>
|
|
|
|
|
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(setup =>
|
|
|
|
|
{
|
|
|
|
|
setup.EnableDeepLinking();
|
|
|
|
|
setup.DisplayRequestDuration();
|
|
|
|
|
setup.ShowCommonExtensions();
|
|
|
|
|
setup.ShowExtensions();
|
|
|
|
|
setup.EnableFilter();
|
|
|
|
|
});
|
|
|
|
|
app.UseCors("AllowAll");
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
app.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|