【发布时间】:2016-12-09 02:51:53
【问题描述】:
我尝试开始使用带有 SPA 的 asp.net 核心 Web 应用程序。 我已经按照教程构建了所有内容。所以我这样设置授权:
app.UseIdentity()
.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
我有 web-api 控制器:
[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");
}
//...
}
但是当我在 JS 中调用我的 webapi 端点时,我收到重定向到登录页面而不是 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。对于 MVC 和 WebApi 控制器,如何实现与 AuthorizeAttribute 不同的行为?
感谢您的任何提前。
【问题讨论】:
标签: c# asp.net asp.net-core asp.net-core-mvc asp.net-authorization