在 ASP.NET Core-5 Web API 中,我有:
public static class AuthExtension
{
public static void AddJwtAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<DDMDbContext>();
//services.AddIdentity<IdentityUser, ApplicationRole>()
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 2;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
// ===== Add Jwt Authentication ========
// Adding Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// Adding Jwt Bearer
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = configuration["JWT:ValidAudience"],
ValidIssuer = configuration["JWT:ValidIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JWT:Secret"]))
};
});
}
}
public static class WebExtension
{
public static void AddMvcCoreFramework(this IServiceCollection services, IConfiguration configuration)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest)
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
};
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
}
}
public static IServiceCollection AddVersioning(this IServiceCollection services)
{
services.AddApiVersioning(
options =>
{
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
options.ReportApiVersions = true;
});
services.AddVersionedApiExplorer(
options =>
{
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
// note: the specified format code will format the version as "'v'major[.minor][-status]"
options.GroupNameFormat = "'v'VVV";
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
// can also be used to control the format of the API version in route templates
options.SubstituteApiVersionInUrl = true;
});
return services;
}
public static IServiceCollection AddSwagger(this IServiceCollection services)
{
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
services.AddSwaggerGen(options =>
{
options.OperationFilter<SwaggerDefaultValues>();
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Scheme = "Bearer",
Description = "Enter 'Bearer' following by space and JWT Token.",
Name = "Authorization",
Type = SecuritySchemeType.Http
});
options.OperationFilter<SwaggerAuthorizeCheckOperationFilter>();
// integrate xml comments
options.IncludeXmlComments(XmlCommentsFilePath);
});
return services;
}
appsettings.json:
"JWT": {
"ValidAudience": "http://localhost:4200",
"ValidIssuer": "http://localhost:44358",
"Secret": "kkkkkSS1234"
}
launchSettings.json:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:38845",
"sslPort": 44358
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"DDM.API.Web": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
这是We API的启动类,我在其中调用了扩展
启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddDb(Configuration);
services.AddJwtAuthentication(Configuration);
services.AddMvcCoreFramework(Configuration);
services.AddVersioning();
services.AddSwagger();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
我得到了这个错误:
找不到此本地主机页
未找到网址的网页:https://localhost:44358/
如何解决此错误?
谢谢
默认情况下,它将显示404页面,因为Swagger UI页面未配置为默认页面。尝试访问/swagger
您将获得swagger UI页面。或者您可以这样修改代码。
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
}
现在它将显示swagger页面为默认页面。
您必须将AddController()添加到服务,类似于这样
services.AddControllers()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
并确定默认路由
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
我升级了我的项目,有2个类库和一个MVC项目,从2.2版本升级到了Mvc核心3.0版本 > 更改.net<代码> 2 .像这样改变 <代码> 3.my satrtup.cs ` < br >应用程序。usehttps redirection();app。UseStaticFiles();app。UseDefaultFiles();app。UseCookiePolicy(); 我的程序. cs pu
我目前正在学习。NET核心,我正试图在ASP.NET核心MVC 3.1中使用身份框架进行用户管理。在< code >控制器中,我有一个< code > RoleManagerController ,带有< code>EditRole GET和POST,如下所示: 基于GET,我在文件夹中创建了一个名为的视图,如下所示: 这是我在中配置的内容.cs 但是当我尝试编辑角色并使用角色id访问路线时,页面
下面是pom.xml 日志清楚显示为Tomcat服务器以8080端口号启动 2018-12-19 17:04:35.198信息709136---[nio-8080-exec-1]O.s.web.Servlet.DispatcherServlet:初始化Servlet“Dispatcher Servlet”2018-12-19 17:04:35.198信息709136---[nio-8080-exe
我创建了一个Asp。NET核心Web应用程序。我的应用程序在调试100%上运行,但在发布和主机上运行。未找到子文件夹中的视图 视图 设置 ----
问题内容: 我在不同的机器上使用Selenium来自动化MVC Web应用程序的测试。 我的问题是我无法获取每台计算机的基本URL。 我可以使用以下代码获取当前网址: 但这对我需要导航到其他页面没有帮助。 理想情况下,我可以使用以下内容导航到不同的页面: 我正在使用的可能的解决方法是: 有没有更好的方法可以做到这一点? 问题答案: 解决此问题的最佳方法是创建URL实例。 这是因为.NET中的 类已
问题内容: 我不知道为什么Django无法在我的应用程序中找到请求的URL。 这是我得到的错误代码: 这是我的 sms.urls.py 文件: 这是应用程序的 urls.py 文件: 我找不到我犯的错误。有任何想法吗? 问题答案: 问题是此网址格式中的美元符号。 脱字符号匹配字符串的开头,美元匹配字符串的结尾,因此仅匹配索引URL 。 您应该删除美元并将其更改为: