群友提供的某公司的.NET面试题,他们说看到这种试题就想走。小编把面试题给大家贡献出来,供大家参考。
先列出试题,大家尝试做一做。
面试题
1、startup 类是什么?在哪里调用 startup 类?configservice、configure 方法是什么?
2、描述一下依赖注入后的服务生命周期?
3、说说脚本在请求 Web CoreApi 的时候,为什么会发生跨域问题?如何解决?
4、说说你了解到的鉴权授权技术。
参考答案:
ConfigureServices
和 Configure
方法是什么?Startup
类是 ASP.NET Core 应用程序的入口之一,负责配置应用程序的服务和请求处理管道。
它在应用启动时由 Web 主机(Web Host)或泛型主机(Generic Host) 进行调用。
ASP.NET Core 在 Program.cs
里 创建和配置 WebHost 时,会调用 Startup
类的构造函数,然后执行其 ConfigureServices
和 Configure
方法。例如:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
在这个过程中,Startup
类的 ConfigureServices
和 Configure
方法会被调用。
ConfigureServices
方法作用:注册和配置应用程序所需的服务(依赖注入 DI)。
在 ConfigureServices
方法中,我们可以向 IoC 容器(依赖注入容器)中添加 服务,例如:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>();
services.AddScoped<IMyService, MyService>();
}
services
参数是 IServiceCollection 类型,ASP.NET Core 通过它管理应用的依赖关系。
Configure
方法作用:配置 HTTP 请求处理管道(中间件)。
在 Configure
方法中,我们可以定义 中间件(Middleware) 的执行顺序,例如:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
这里 UseRouting()
、UseAuthorization()
、UseEndpoints()
就是 中间件,它们决定了请求如何被处理。
ASP.NET Core 提供 三种依赖注入(DI)生命周期:
Transient(瞬时)
services.AddTransient<IMyService, MyService>();
Scoped(作用域)
services.AddScoped<IMyService, MyService>();
Singleton(单例)
services.AddSingleton<IMyService, MyService>();
使用 CORS(跨域资源共享)在 ConfigureServices
方法中,启用 CORS:
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
在 Configure
方法中应用 CORS:
app.UseCors("AllowAll");
使用 Response Header 允许跨域服务器响应时添加:
Response.Headers.Add("Access-Control-Allow-Origin", "*");
使用代理(Proxy)转发请求
JSONP(仅支持 GET 请求)
<script>
标签发送 JSONP 请求,但现代 API 通常不推荐使用 JSONP。在 .NET Core 中,认证(Authentication) 和 授权(Authorization) 是 Web 安全的核心部分。
鉴权是 确认用户身份 的过程,常见方法包括:
JWT(JSON Web Token)
Authorization: Bearer <token>
头部。JwtBearer
进行验证:services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("YourSecretKey"))
};
});
Cookie 认证
Set-Cookie
,后续请求自动带上 Cookie
。OAuth2 + OpenID Connect
Microsoft.AspNetCore.Authentication.OpenIdConnect
进行集成。授权是 确认用户是否有权限访问资源 的过程,常见方式包括:
基于角色的授权(Role-based Authorization)
[Authorize(Roles = "Admin")]
public IActionResult AdminOnly()
{
return Ok("只有管理员可以访问");
}
基于策略的授权(Policy-based Authorization)
services.AddAuthorization(options =>
{
options.AddPolicy("AgeOver18", policy =>
policy.RequireAssertion(context =>
context.User.HasClaim(c => c.Type == "Age" && int.Parse(c.Value) >= 18)));
});
控制器中应用:
[Authorize(Policy = "AgeOver18")]
public IActionResult AdultOnly()
{
return Ok("成年人才能访问");
}
基于声明的授权(Claims-based Authorization)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有