代码拉取完成,页面将自动刷新
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenAiClient4ChatGPT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using WebSocket_FleckMsg.Filters;
using WebSocket_FleckMsg.Filters.Common;
using WebSocket_FleckMsg.MessageService;
using WebSocket_FleckMsg.Model;
using WebSocket_FleckMsg.Services.interfaces;
using WebSocket_FleckMsg.Services.lmpl;
using WebSocket_FleckMsg.Services.RedisService;
namespace WebSocket_FleckMsg
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddControllers();
//services.AddDbContext<FleckContext>();
services.AddDbContext<FleckContext>(optionsAction =>
{
optionsAction.UseMySQL(Configuration.GetConnectionString("MysqlConnection"));
});
// 全局注册 过滤器 在ConfigureServices 添加
services.AddMvc(option => { option.Filters.Add(typeof(FleckActionFilter)); });
services.AddMvc(option => { option.Filters.Add(typeof(FleckExceptionFilter)); });
services.AddMvc(option => { option.Filters.Add(typeof(FleckLoginVerifyAttribute)); });
services.AddScoped<IUser, UserService>();
services.AddSingleton<IMYRedisHelper, MYRedisHelper>();
services.AddSingleton<IMessageInfo, MessageInfoService>();
services.AddSingleton<IWebSocketServerService, WebSocketServerService>();
services.AddScoped<IFilmDetail, FilmDetailService>();
services.AddSingleton<IMessageInfo, MessageInfoService>();
services.AddHttpClient();
services.AddSingleton<IOpenAiServices, OpenAiServices>();
services.AddCors(options =>
{
options.AddPolicy("AnyOrigin", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
//认证
services.AddAuthentication(delegate (AuthenticationOptions options)
{
options.AddScheme<DefaultAuthenticationHandler>("HexAuthScheme", "hex auth scheme");
});
services.AddHttpContextAccessor();
//授权 {RealName: pre张晓玉}
services.AddAuthorization(options =>
{
// 基于声明策略授权 自定义策略
// options.AddPolicy("HexAuthScheme2", policy => policy.RequireClaim("RealName", "pre张晓玉"));
// options.AddPolicy("HexAuthScheme2", policy => policy.RequireClaim("age", "23","22","21"));
//基于角色策略授权 只有 Role 为 pre张晓玉 的可以访问
//options.AddPolicy("HexAuthScheme3", policy => policy.RequireRole("pre张晓玉"));
// 基于多个角色策略授权
//options.AddPolicy("ee", policy => policy.RequireRole("ff", "gg"));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("AnyOrigin");
/* app.Use(async (context, next) =>
{
AuthenticateResult result = await context.AuthenticateAsync("HexAuthScheme");
if (result?.Principal != null)
{
context.User = result.Principal;
}
await next();
});*/
// 注意下面 Routing Authentication Authorization 这三个中间件的放置顺序,必须按照这个顺序:
app.UseRouting();
// 添加 身份验证 中间件(注意顺序,中间件这里是:先身份验证再授权)、
// 而且 身份验证 和 授权 都要放在Routing 之后
//认证):常用的认证方式有用户名密码认证。
app.UseAuthentication();
//授权):明确你是否有某个权限。当用户需要使用某个功能的时候,系统需要校验用户是否需要这个功能的权限。
/*
* 通常和控制器对应
* [ApiController]
[Route("api/[controller]")]
[Authorize]
* **/
app.UseAuthorization();
//启动WebSocket服务
var webSocketServerService = app.ApplicationServices.GetService<IWebSocketServerService>();
webSocketServerService.Startup();
/* app.UseDefaultFiles();
app.UseFileServer();*/
app.UseStaticFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。