私はSPAを使用してasp.netコアWebアプリケーションを開始しようとします。 私はチュートリアルですべてを構築しました。だから、そのような私のセットアップの許可:WebアプリケーションのリダイレクトのためのAsp.netコア認可
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");
}
//...
}
しかし、私はJSで私WEBAPIエンドポイントを呼び出すときに、 401
ステータスの代わりにログインページにリダイレクトされます。
私は調査を始めて、false
をAutomaticChallenge
に設定し、.UseIdentity()
を削除しなければならないというstackoverflowに関する回答が見つかりました。しかし、私がそれをするとき、私の[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
? ありがとうございます。