Asp.net core 表单POST调用使用Active Directory身份验证在ASP.NET核心控制器中获取操作

Asp.net core 表单POST调用使用Active Directory身份验证在ASP.NET核心控制器中获取操作,asp.net-core,asp.net-core-mvc,azure-active-directory,Asp.net Core,Asp.net Core Mvc,Azure Active Directory,窗体,在调用控制器时,将向控制器操作发送的消息转换为获取请求。以下是为验证method=“post”而在浏览器(F12)中呈现的内容 .... 然后,由于某种原因,重定向到公司Azure AD身份验证开始,即使此特定控制器在任何地方都没有[Authorize]属性 然后在post之后,调用此[HttpGet]方法,而不是[HttpPost]方法。(我能理解为什么在返回到我的网站控制器之前,浏览器正在重定向到上图中的URL时,GET会被调用,但不确定如何控制此行为) [HttpGet] 公共

窗体,在调用控制器时,将向控制器操作发送的消息转换为获取请求。以下是为验证method=“post”而在浏览器(F12)中呈现的内容


....
然后,由于某种原因,重定向到公司Azure AD身份验证开始,即使此特定控制器在任何地方都没有[Authorize]属性

然后在post之后,调用此[HttpGet]方法,而不是[HttpPost]方法。(我能理解为什么在返回到我的网站控制器之前,浏览器正在重定向到上图中的URL时,GET会被调用,但不确定如何控制此行为)

[HttpGet]
公共异步任务AcceptTermsAndConditions(字符串失败重定向URL)
下面的代码永远不会执行

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> AcceptTermsAndConditions(IFormCollection collection)
    {
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务AcceptTermsAndConditions(IFormCollection集合)
{
此外,如果我检查请求对象,请求方法是“GET”而不是POST


我做错了什么???

最终,我加入了一个中间件类,我昨天刚刚注释掉了这个类,由于我的日程安排有点紧,所以问题没有报告

但是,这段代码导致的问题实际上是针对API服务的,现在可以理解:

//This middleware offers source authentication prior to routing to page controllers if the call is to an API controller
app.UseMiddleware<SourceAuthentication>();
最后一行导致了问题! 因此,在POST过程中,响应是立即的401,而没有到达请求管道的MVC部分。
此中间件对于REST API调用非常有效,但对MVC视图控制器活动有害。

如果在其中创建示例控制器和httppost操作方法并尝试调用该操作,会发生什么情况?该操作是否正常工作?是否启用了允许将AuthorizeAttribute筛选器添加到global.asax文件中的protect每个控制器的每个操作方法。Muqet,我确实通过一个Scaffold新项创建了一个示例控制器和httppost操作方法-以防我丢失了一些东西,但它仍然存在相同的问题。创建和编辑不会发布并始终调用这些的GET操作。我只在使用Active Directory时遇到这个问题身份验证Nan Yu,我不知道在MVC Core中使用global.asax。我在“类”中使用[Authorize]和[AllowAnonymous]level.muqet,我用Active Directory身份验证创建了一个全新的项目,构建了新的Controller+View+EntityFramework,它成功了。我发现我们的定制中间件是如何导致这个问题的
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> AcceptTermsAndConditions(IFormCollection collection)
    {
//This middleware offers source authentication prior to routing to page controllers if the call is to an API controller
app.UseMiddleware<SourceAuthentication>();
public async Task Invoke(HttpContext context, AspNetUsersContext usersContext, SourceLogContext sourceContext)
{
   string authHeader = context.Request.Headers["Authorization"];
   if (context.Request.Method.ToString() == "GET")
   {
       // Log the GET access here, etc. and continue
   …. 
       await _next.Invoke(context);
       return;
    }

    if (authHeader != null && authHeader.StartsWith("Basic"))
    {
        //Extract credentials
        string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
        var encoding = Encoding.GetEncoding("iso-8859-1");
        string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));

        int seperatorIndex = usernamePassword.IndexOf(':');

        var username = usernamePassword.Substring(0, seperatorIndex);
        var password = usernamePassword.Substring(seperatorIndex + 1);

        var source = usersContext.AspNetUsers.SingleOrDefault(r => r.UserName == username && r.PasswordHash == password);

        if (source != null)
        {
            // If found source creds, then log and continue…………
            await _next.Invoke(context);
        }
        else
        {
            context.Response.StatusCode = 401; //Unauthorized
            return;
        }
    }
    else
    {
        // This Caused the Error
        // no authorization header
        context.Response.StatusCode = 401;
        return;
    }
}