Ai
3 Star 3 Fork 1

likang/WebRTC后端

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Startup.cs 5.45 KB
一键复制 编辑 原始数据 按行查看 历史
likang 提交于 2023-06-19 19:16 +08:00 . feat:更新了已知内容
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();
});
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/kkacoder/web-rtc-backend.git
git@gitee.com:kkacoder/web-rtc-backend.git
kkacoder
web-rtc-backend
WebRTC后端
master

搜索帮助