aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Startup.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-07-27 21:47:14 +0800
committerGitHub <noreply@github.com>2019-07-27 21:47:14 +0800
commit590a8c576f17817539505ef2ca50f52e840a61d2 (patch)
tree572a2ae5c65c484718b3bfda68fd8babc56fe6f2 /Timeline/Startup.cs
parent3de4179449a209646e0e5a967d270f7fa0878c03 (diff)
parent58985e8f2a6931029974067b2c1e78963e4508f0 (diff)
downloadtimeline-590a8c576f17817539505ef2ca50f52e840a61d2.tar.gz
timeline-590a8c576f17817539505ef2ca50f52e840a61d2.tar.bz2
timeline-590a8c576f17817539505ef2ca50f52e840a61d2.zip
Merge pull request #25 from crupest/auth
Refactor a lot, especially authentication.
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r--Timeline/Startup.cs39
1 files changed, 5 insertions, 34 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs
index acabe55c..a6965190 100644
--- a/Timeline/Startup.cs
+++ b/Timeline/Startup.cs
@@ -1,4 +1,3 @@
-using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
@@ -7,11 +6,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
-using Microsoft.IdentityModel.Tokens;
-using System.Text;
-using System.Threading.Tasks;
+using Timeline.Authenticate;
using Timeline.Configs;
-using Timeline.Formatters;
using Timeline.Models;
using Timeline.Services;
@@ -33,10 +29,7 @@ namespace Timeline
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
- services.AddMvc(options =>
- {
- options.InputFormatters.Add(new StringInputFormatter());
- }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
+ services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(options =>
{
@@ -52,29 +45,8 @@ namespace Timeline
services.Configure<JwtConfig>(Configuration.GetSection(nameof(JwtConfig)));
var jwtConfig = Configuration.GetSection(nameof(JwtConfig)).Get<JwtConfig>();
- services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
- .AddJwtBearer(o =>
- {
- o.Events = new JwtBearerEvents
- {
- OnMessageReceived = delegate (MessageReceivedContext context)
- {
- context.Request.Query.TryGetValue("token", out var value);
- if (value.Count == 1)
- {
- context.Token = value[0];
- }
- return Task.CompletedTask;
- }
- };
- o.TokenValidationParameters.ValidateIssuer = true;
- o.TokenValidationParameters.ValidateAudience = true;
- o.TokenValidationParameters.ValidateIssuerSigningKey = true;
- o.TokenValidationParameters.ValidateLifetime = true;
- o.TokenValidationParameters.ValidIssuer = jwtConfig.Issuer;
- o.TokenValidationParameters.ValidAudience = jwtConfig.Audience;
- o.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.SigningKey));
- });
+ services.AddAuthentication(AuthConstants.Scheme)
+ .AddScheme<AuthOptions, AuthHandler>(AuthConstants.Scheme, AuthConstants.DisplayName, o => { });
services.AddScoped<IUserService, UserService>();
services.AddScoped<IJwtService, JwtService>();
@@ -96,8 +68,7 @@ namespace Timeline
services.AddHttpClient();
- services.Configure<QCloudCosConfig>(Configuration.GetSection(nameof(QCloudCosConfig)));
- services.AddSingleton<IQCloudCosService, QCloudCosService>();
+ services.AddMemoryCache();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.