Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core MVC6OpenIDConnect_Asp.net Core_Asp.net Core Mvc_Openid Connect - Fatal编程技术网

Asp.net core MVC6OpenIDConnect

Asp.net core MVC6OpenIDConnect,asp.net-core,asp.net-core-mvc,openid-connect,Asp.net Core,Asp.net Core Mvc,Openid Connect,我目前在将我的MVC应用程序从beta 3迁移到4时遇到了多个问题—其中一个问题与OpenIdConnect到Windows Azure进行身份验证有关。当我转到一个具有Authorize属性的页面时,该页面停止处理并位于一个空白的白色页面上,而不显示Azure登录页面。我没有得到一个ySOD -只是空白屏幕。至于示例代码,我只能找到以下代码: 如果我使用第二个示例,并且实际在另一个控制器中使用ChallengeResult,它确实会打开Azure登录页面,但会在Azure端返回错误请求(40

我目前在将我的MVC应用程序从beta 3迁移到4时遇到了多个问题—其中一个问题与OpenIdConnect到Windows Azure进行身份验证有关。当我转到一个具有Authorize属性的页面时,该页面停止处理并位于一个空白的白色页面上,而不显示Azure登录页面。我没有得到一个ySOD -只是空白屏幕。至于示例代码,我只能找到以下代码:

如果我使用第二个示例,并且实际在另一个控制器中使用ChallengeResult,它确实会打开Azure登录页面,但会在Azure端返回错误请求(400)

这是我当前的代码:

public void ConfigureServices(IServiceCollection services)
{
    // Cannot find services.AddAuthentication that is supposed to be in Microsoft.Framework.DependencyInjection
    services.AddWebEncoders(); 
    services.AddDataProtection();

services.Configure<ExternalAuthenticationOptions>(options =>
{
    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationType;
});

// Add MVC services to the services container.
services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
{
    // Configure the OWIN Pipeline to use OpenID Connect Authentication
    app.UseCookieAuthentication(options => { 
        options.AutomaticAuthentication = true;
    });

    app.UseOpenIdConnectAuthentication(options =>
    {
        options.ClientId = Constants.ClientId;
        options.Authority = Constants.Authority;
        options.PostLogoutRedirectUri = Constants.PostLogoutRedirectUri;
        options.TokenValidationParameters.RoleClaimType = "roles";

        options.Notifications = new OpenIdConnectAuthenticationNotifications()
        {
              AuthorizationCodeReceived = async (context) =>
              {
                  var code = context.Code;

                  ClientCredential credential = new ClientCredential(Constants.ClientId, Constants.AppKey);

                  AuthenticationContext authContext = new AuthenticationContext(Constants.Authority, false);
                  var result = await authContext.AcquireTokenByAuthorizationCodeAsync(
                            code, new Uri(Constants.PostLogoutRedirectUri), credential, Constants.GraphUri);
                  ActiveDirectoryHelper.token = result.AccessToken;
                }
          };
     });

     // More MVC stuff such as routing and static files
}
public void配置服务(IServiceCollection服务)
{
//找不到应位于Microsoft.Framework.DependencyInjection中的services.AddAuthentication
services.AddWebEncoders();
services.AddDataProtection();
配置(选项=>
{
options.signnscheme=CookieAuthenticationDefaults.AuthenticationType;
});
//将MVC服务添加到服务容器中。
services.AddMvc();
}
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
//将OWIN管道配置为使用OpenID连接身份验证
app.UseCookieAuthentication(选项=>{
options.AutomaticAuthentication=true;
});
app.UseOpenIdConnectAuthentication(选项=>
{
options.ClientId=Constants.ClientId;
options.Authority=Constants.Authority;
options.postlogutredirecturi=Constants.postlogutredirecturi;
options.TokenValidationParameters.RoleClaimType=“角色”;
options.Notifications=新的OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived=异步(上下文)=>
{
var code=context.code;
ClientCredential=新的ClientCredential(Constants.ClientId,Constants.AppKey);
AuthenticationContext authContext=新的AuthenticationContext(Constants.Authority,false);
var result=await authContext.AcquireTokenByAuthorizationCodeAsync(
代码,新Uri(Constants.postlogutredirectURI),凭证,Constants.GraphUri);
ActiveDirectoryHelper.token=result.AccessToken;
}
};
});
//更多MVC内容,如路由和静态文件
}

还有,有人对MVC6有什么有用的资源吗?我一直在搜索GitHub的大部分Beta 4代码。

您遇到的问题与Cookie标头有关,如果您得到的400错误是“HTTP错误400。请求标头的大小太长。”,则超出了HTTP.sys的限制。Azure AD cookie标头可能超过单个标头的限制。我也有同样的问题,这是我的饼干:

ARRAffinity=65字节

.AspNet.Cookies=9字节

.AspNet.CookiesC1=4046字节

.AspNet.CookiesC2=4046字节

.AspNet.CookiesC3=4046字节

.AspNet.CookiesC4=3850字节

您可能会看到类似的图片。有两种变通方法:

  • 如果您可以控制服务器,请申请突破限制并重新启动服务器

  • 如果您无法控制服务器(例如Azure Web应用程序),则需要缩小cookie的大小。要做到这一点,您可以将cookie内容存储在ASP.NET 5会话上,而是存储小得多的会话cookie。例如:

    app.UseCookieAuthentication(options =>
    {
        options.AutomaticAuthentication = true;
        options.SessionStore = new MemoryCacheSessionStore();
    });
    
    下面是的一个实现。您可以在ASP.NET安全存储库中找到完整的示例


  • 谢谢-这就解决了这个问题。现在谈谈其余的问题:)在使用
    [Authorize]
    时,您知道如何解决空响应问题了吗?我也在同一条船上。