加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – MVC 6 OpenIdConnect

发布时间:2020-12-16 09:55:06 所属栏目:asp.Net 来源:网络整理
导读:我目前遇到了将MVC应用程序从beta 3迁移到4的多个问题 – 其中一个问题与OpenIdConnect到 Windows Azure进行身份验证有关.当我转到具有“授权”属性的页面时,页面将停止处理并位于空白页面,而不会显示Azure登录页面.我没有得到YSOD – 只是空白屏幕.至于示例
我目前遇到了将MVC应用程序从beta 3迁移到4的多个问题 – 其中一个问题与OpenIdConnect到 Windows Azure进行身份验证有关.当我转到具有“授权”属性的页面时,页面将停止处理并位于空白页面,而不会显示Azure登录页面.我没有得到YSOD – 只是空白屏幕.至于示例代码,我只能找到这些:
https://github.com/aspnet/Security/blob/5cf0564484cf5bb2a7a16e6485816d19287538e6/samples/OpenIdConnectSample/Startup.cs
https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/vNext/samples/Mvc/Mvc.Client/Startup.cs

如果我使用第二个示例,并且实际在不同的控制器中使用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
}

附:有没有人为MVC 6提供任何有用的资源?我一直在为GitHub搜索我的大部分Beta 4代码.

解决方法

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

ARRAffinity = 65 bytes

.AspNet.Cookies = 9 bytes

.AspNet.CookiesC1 = 4046 bytes

.AspNet.CookiesC2 = 4046 bytes

.AspNet.CookiesC3 = 4046 bytes

.AspNet.CookiesC4 = 3850 bytes

您可能会看到类似的图片.有几种解决方法:

>如果您可以控制服务器,请应用these registry changes以突破限制并重新启动服务器.
>如果您无法控制服务器(例如Azure Web Apps),则需要缩小cookie的大小.为此,您可以将cookie内容存储在ASP.NET 5会话中,而是存储更小的会话cookie.例:

app.UseCookieAuthentication(options =>
{
    options.AutomaticAuthentication = true;
    options.SessionStore = new MemoryCacheSessionStore();
});

MemoryCacheSessionStore这里是IAuthenticationSessionStore的实现.您可以在ASP.NET安全库中找到完整的示例here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读