.net 无法获取任何响应-发送HTTP标头后,服务器无法追加标头

.net 无法获取任何响应-发送HTTP标头后,服务器无法追加标头,.net,asp.net-mvc,iis,owin,.net,Asp.net Mvc,Iis,Owin,我有一个.NETMVC项目,我在项目中使用API控制器来实现RESTAPI。本项目中使用的身份验证基于OWIN中间件 以下代码段在OwinMiddleware中用于身份验证 var authValue = context.Request.Headers["Authorization"]; if (!string.IsNullOrEmpty(authValue) && (authValue.StartsWith("Basic "))) { authValue =

我有一个.NETMVC项目,我在项目中使用API控制器来实现RESTAPI。本项目中使用的身份验证基于OWIN中间件

以下代码段在OwinMiddleware中用于身份验证

 var authValue = context.Request.Headers["Authorization"];
 if (!string.IsNullOrEmpty(authValue) && (authValue.StartsWith("Basic ")))
  {
      authValue = authValue.Substring(6).Trim();
      authValue = Encoding.UTF8.GetString(Convert.FromBase64String(authValue));
      var credentials = authValue.Split(":".ToCharArray());
      var signInStatus = await _signinManager.PasswordSignInAsync(credentials[0], credentials[1], false, false);

          switch (signInStatus)
           {
             case Microsoft.AspNet.Identity.Owin.SignInStatus.Success:
                    var user = await _userManager.FindByNameAsync(credentials[0]);
                    var identity = await _signinManager.CreateUserIdentityAsync(user);
                    context.Request.User = new ClaimsPrincipal(identity);
                     await base.Next.Invoke(context);
                     break;

             default:
                   context.Response.StatusCode = 401;
// an aobject representing error
                   var res = GetResposeData() 
                   var format = GetResponseContentType(context.Request);
                   var data = ParseToString(res , format);
                   var dataByte = Encoding.UTF8.GetBytes(data);
                  context.Response.ContentType = GetContentType(format);
                  context.Response.Body.Write(dataByte, 0, dataByte.Length);
                   break;
             }

    }
在startup.cs中,我添加了以下代码

public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<CustomSignInManager>());
        app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<CustomUserManager>());
        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/account/index"),
            Provider = new CookieAuthenticationProvider()
            {
                OnApplyRedirect = ctx =>
                {
                    ctx.Response.Redirect(ctx.RedirectUri);
                }
            }
        });
        app.UseAutofacMiddleware(AutofacConfig.Container);
    }

您混淆了webapi和MVC身份验证。将代码startup.cs更改为以下内容:

public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<CustomSignInManager>());
        app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<CustomUserManager>());
        app.MapWhen((context) => {
                return !IsApiRequest(context.Request);                
            }, (mvcApp) => {
                mvcApp.UseCookieAuthentication(new CookieAuthenticationOptions()
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/account/index"),
                    Provider = new CookieAuthenticationProvider()
                });
            });
        app.UseAutofacMiddleware(AutofacConfig.Container);
    }
public void ConfigureAuth(IAppBuilder应用程序)
{
app.CreatePerOwinContext(()=>DependencyResolver.Current.GetService());
app.CreatePerOwinContext(()=>DependencyResolver.Current.GetService());
app.MapWhen((上下文)=>{
return!IsApiRequest(context.Request);
},(mvcApp)=>{
mvcApp.UseCookieAuthentication(新的CookieAuthenticationOptions()
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/account/index”),
Provider=新CookieAuthenticationProvider()
});
});
app.UseAutofacMiddleware(AutofacConfig.Container);
}
public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<CustomSignInManager>());
        app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<CustomUserManager>());
        app.MapWhen((context) => {
                return !IsApiRequest(context.Request);                
            }, (mvcApp) => {
                mvcApp.UseCookieAuthentication(new CookieAuthenticationOptions()
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/account/index"),
                    Provider = new CookieAuthenticationProvider()
                });
            });
        app.UseAutofacMiddleware(AutofacConfig.Container);
    }