是否有办法使用Razor页面代码来加载静态文件,就像我可以使用传统的MVC控制器一样?今天早上我已经玩了几个小时了,似乎无法找到实现这个目标的方法。任何输入都表示赞赏!
Razor Page Code Behind:
public async void OnGetAsync()
{
var request = HttpContext.Request;
var sessionId = request.Query.FirstOrDefault().Value;
var session = await _httpService.ValidateSession(sessionId);
if (!string.IsNullOrEmpty(session?.UserId))
{
var claims = new List<Claim>()
{
new Claim(CustomClaimTypes.UserId, session.UserId),
new Claim(CustomClaimTypes.BuId, session.BuId),
new Claim(CustomClaimTypes.SecurityLevel, session.SecurityLevel)
};
var identity = new ClaimsIdentity(claims, "TNReadyEVP");
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal,
new AuthenticationProperties { ExpiresUtc = DateTime.Now.AddMinutes(60), IsPersistent = true, AllowRefresh = false });
var isAuthenticated = principal.Identity.IsAuthenticated;
Redirect("~/wwwroot/index.html");## Heading ##
}
else
{
RedirectToPage("./Error");
}
MVC控制器
public async Task<IActionResult> SignIn()
{
var sessionId = HttpContext.Request.Query.FirstOrDefault().Value;
var session = await _httpService.ValidateSession(sessionId);
if (!string.IsNullOrEmpty(session?.UserId))
{
var claims = new List<Claim>()
{
new Claim(CustomClaimTypes.UserId, session.UserId),
new Claim(CustomClaimTypes.BuId, session.BuId),
new Claim(CustomClaimTypes.SecurityLevel, session.SecurityLevel)
};
var identity = new ClaimsIdentity(claims, "TNReadyEVP");
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal,
new AuthenticationProperties { ExpiresUtc = DateTime.Now.AddMinutes(20), IsPersistent = true, AllowRefresh = false });
var isAuthenticated = principal.Identity.IsAuthenticated;
}
else
{
return Forbid();
}
return View("~/wwwroot/index.html");
}
答案 0 :(得分:1)
首先,我想说Core 2在报告错误方面非常糟糕。有时会这样做,有时代码会失败而没有异常报告。除了Core 2很棒。
以下是答案,您将看到我更改了方法签名以返回IActionResult,从而可以使用RedirectToPage和File。
public async Task<IActionResult> OnGetAsync()
{
var request = HttpContext.Request;
var sessionId = request.Query.FirstOrDefault().Value;
var session = await _httpService.ValidateSession(sessionId);
if (!string.IsNullOrEmpty(sessionId))
{
var claims = new List<Claim>()
{
new Claim(CustomClaimTypes.UserId, session.UserId),
new Claim(CustomClaimTypes.BuId, session.BuId),
new Claim(CustomClaimTypes.SecurityLevel, session.SecurityLevel)
};
var identity = new ClaimsIdentity(claims, "TNReadyEVP");
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal,
new AuthenticationProperties { ExpiresUtc = DateTime.Now.AddMinutes(60), IsPersistent = true, AllowRefresh = false });
var isAuthenticated = principal.Identity.IsAuthenticated;
return File("index.html", "text/html");
}
else
{
return RedirectToPage("Error");
}
}
答案 1 :(得分:0)
我怀疑问题的根源是:
public async void OnGetAsync()
您正在分派尚未等待的异步操作,并且上下文将被处置-请参见cannot access a disposed object asp net identitycore和aspnet Mvc issues 7011
FIX:始终使用
public async Task OnGetAsync()
我回想起有关报告错误的想法,而不是将头埋在沙子里。在这种情况下,ASP.NET Core 2.2无法报告该错误,这意味着在更改方法签名之前,您将遇到很多奇怪的问题。