我尝试开始与SPA的asp.net核心web应用程序。 我已经通过教程构建了一切。所以我设置的授权这样的:Asp.net核心授权web-api发射重定向
app.UseIdentity()
.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
而且我的WebAPI控制器:
[Route("Somewhere")]
[Produces("application/json")]
[Authorize()]
public class MyControllerController : Controller
{
[HttpGet]
public async Task<IEnumerable<Something>> GetSomething()
{
//....
}
}
和授权功能:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
//...
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe);
if (result.Succeeded)
{
_logger.LogInformation(1, "User logged in.");
return Redirect("somewhere");
}
//...
}
但是,当我把我的WebAPI端点JS,我收到重定向到登录页面而不是401
状态。
我已经开始调查,并找到了stackoverflow的答案,我必须将false
设置为AutomaticChallenge
并删除.UseIdentity()
。但是,当我这样做时,我的[POST]AccountController.Login
方法停止在线工作 - var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe);
但有例外 - No authentication handler is configured to handle the scheme: Identity.Application
。
我想在我的MVC控制器中接收重定向,但在没有授权的情况下从Webapi端点收到401/403。如何实现从AuthorizeAttribute
对MVC和WebApi控制器的不同行为? 感谢您的任何提前。