我正在尝试创建类似于Blog // PersonalPortal // github.com的应用程序
我复制了一部分应用程序。
我正在调试。
出现开始页面,然后在3-5秒后调试停止。
问题。
为什么调试停止?
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebAppCore
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Startup.cs
//
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace WebAppCore
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// services.AddControllersWithViews();
}
// 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.UseRouting();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapGet("/", async context =>
// {
// await context.Response.WriteAsync("Hello World!");
// });
//});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
HomeController.cs
using Microsoft.AspNetCore.Mvc;
namespace PersonalPortal.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
Index.cshtml
@page
@model WebAppCore.Views.Home.IndexModel
@{
}
<!DOCTYPE html>
<html>
<body>
<h1>*** Тестовая страница ***</h1>
</body>
</html>
BlogController.cs
using Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WebAppCore.Services.Interfaces;
using WebAppCore.ViewModels;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PersonalPortal.Controllers
{
[Route("api/[controller]")]
public class BlogController : Controller
{
IBlogService _blogService;
public BlogController(IBlogService blogService)
{
_blogService = blogService;
}
[Route("page")]
[HttpGet]
public async Task<Page<PostLiteViewModel>> GetPosts(int pageIndex, string tag)
{
return await _blogService.GetPosts(pageIndex, tag);
}
[Route("post")]
[HttpGet]
public async Task<Post> GetPost(int postId)
{
return await _blogService.GetPost(postId);
}
[Route("comment")]
[HttpPost]
public async Task AddComment([FromBody] AddCommentRequest request)
{
await _blogService.AddComment(request);
}
[Authorize]
[Route("comment")]
[HttpDelete]
public async Task DeleteComment(int commentId)
{
await _blogService.DeleteComment(commentId);
}
[Authorize]
[Route("post")]
[HttpPost]
public async Task AddPost([FromBody] AddPostRequest request)
{
await _blogService.AddPost(request);
}
[Authorize]
[Route("post")]
[HttpDelete]
public async Task DeletePost(int postId)
{
await _blogService.DeletePost(postId);
}
[Route("tags")]
[HttpGet]
public async Task<List<string>> GetTags()
{
return await _blogService.GetTags();
}
}
}
IdentityController.cs
//
using WebAppCore.Services.Interfaces;
using WebAppCore.Helpers;
using WebAppCore.ViewModels;
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Linq;
namespace PersonalPortal.Controllers
{
[Route("api/[controller]")]
public class IdentityController : Controller
{
IIdentityService _service;
public IdentityController(IIdentityService service)
{
_service = service;
}
[Route("token")]
[HttpPost]
public async Task<IActionResult> Token([FromBody]IdentityViewModel model)
{
var identity = await GetIdentity(model.Username, model.Password);
if (identity == null)
{
return Unauthorized();
}
var now = DateTime.UtcNow;
// создаем JWT-токен
var jwt = new JwtSecurityToken(
issuer: AuthOptions.ISSUER,
audience: AuthOptions.AUDIENCE,
notBefore: now,
claims: identity.Claims,
expires: now.Add(TimeSpan.FromMinutes(AuthOptions.LIFETIME)),
signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256));
var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
var response = new
{
access_token = encodedJwt,
username = identity.Name
};
return Ok(response);
}
private async Task<ClaimsIdentity> GetIdentity(string userName, string password)
{
ClaimsIdentity identity = null;
var user = await _service.GetUser(userName);
if (user != null)
{
var sha256 = new SHA256Managed();
var passwordHash = Convert.ToBase64String(sha256.ComputeHash(Encoding.UTF8.GetBytes(password)));
if (passwordHash == user.Password)
{
var claims = new List<Claim>
{
new Claim(ClaimsIdentity.DefaultNameClaimType, user.Login),
};
identity = new ClaimsIdentity(claims, "Token", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
}
}
return identity;
}
}
}