当前位置: 首页 > 知识库问答 >
问题:

ASP。NET Core 3.1 MVC添加OpenIDConnect和IdentityServer 3

韶宏邈
2023-03-14

在此问题上的任何帮助都将不胜感激。我在这件事上浪费了好几天时间。

验证ASP。具有IdentityServer3的NET Core 3.1 MVC应用程序导致运行时错误。身份服务器返回错误

客户端应用程序未知或未经授权

而不是登录屏幕。我们有一个ASP。NET MVC 5应用程序和ASP。NET核心API,可与identity server配合使用。

我的方法是重写ASP。NETMVC5中的代码。净核心。我已经尽了最大努力,但没有找到任何关于如何进行此类翻译的文档。有关详细信息,请参阅下面的代码片段。

正在工作的ASP。NET MVC 5代码:

    //***
    //commented all code that was not needed to get login screen to show up
    //***
    public void Configuration(IAppBuilder app)
    {
        AntiForgeryConfig.UniqueClaimTypeIdentifier = IdentityModel.JwtClaimTypes.Name;
        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies",
            ExpireTimeSpan = new TimeSpan(0, 300, 0),
            SlidingExpiration = true
        });

        var clientBaseUrl = ConfigurationManager.AppSettings[ClientBaseUrlKey];
        var identityServerBaseUrl = ConfigurationManager.AppSettings[IdentityServerBaseUrlKey];

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            Authority = identityServerBaseUrl,
            ClientId = WebSettings.ClientId,
            ResponseType = "code id_token token",
            SignInAsAuthenticationType = "Cookies",
            UseTokenLifetime = false//,
            RedirectUri = $"{clientBaseUrl}/",
            //PostLogoutRedirectUri = clientBaseUrl,
            //Scope = "openid profile roles admin_certpay",

            //Notifications = new OpenIdConnectAuthenticationNotifications
            //{

...为了简洁起见...});}

有问题的ASP. NET Core 3.1 MVC代码:

public void ConfigureServices(IServiceCollection services){services.AddControllersWithViews();

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = "Cookies";
        }).AddCookie("Cookies")
        .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, o =>
        {
            o.Authority = "http://localhost/identity/";
            o.ClientId = "actual value used here";
            o.ResponseType = "code id_token token"; 
            o.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            o.UseTokenLifetime = false;
            //start - not sure what RedirectUri is, but PostLogoutRedirectUri doesn't matter
            o.SignedOutRedirectUri = "http://localhost/CertPay.Admin/";
            o.ReturnUrlParameter = "http://localhost/CertPay.Admin/";
            //end - not sure what RedirectUri is, but PostLogoutRedirectUri doesn't matter
            o.RequireHttpsMetadata = false; //fix to runtime error
        });

        //Played with Core API fix for the hell of it.
        //.AddIdentityServerAuthentication(o =>
        //{
        //    o.Authority = "http://localhost/identity/";
        //    //o.ApiName = "actual value here";
        //    o.LegacyAudienceValidation = true;
        //    o.RequireHttpsMetadata = true;
        //});
}

共有1个答案

孙经艺
2023-03-14

这个帖子上的孩子佩德罗给出的答案解决了我的问题。删除RedirectUri属性可以通过添加事件侦听器进行补偿。为方便起见,佩德罗的文章节选如下:

x.Events.OnRedirectToIdentityProvider = async n =>
{
    n.ProtocolMessage.RedirectUri = <Redirect URI string>;
    await Task.FromResult(0);
}

编辑:上面的解决方案实际上导致了登录页面无休止地循环加载多次。以下解决方案没有导致该问题:

o.CallbackPath = "/home/index/";
 类似资料:
  • 注意:用户界面已经在 Dreamweaver CC 和更高版本中做了简化。因此,您可能在 Dreamweaver CC 和更高版本中找不到本文中描述的一些选项。有关详细信息,请参阅此文章。 关于 ASP 命令对象 ASP 命令对象是对数据库执行某种操作的服务器对象。该对象可以包含任何有效的 SQL 语句,包括返回记录集的语句或在数据库中插入、更新或删除记录的语句。如果 SQL 语句添加或删除表格中

  • 我在母版页中有一个表 table border=“1”border style=“shown”width=“80%”id=“tbladdbirthdies”tr id=“tr”td asp:TextBox id=“txFirstName”runat=“server”asp:TextBox id=“txLastName”runat=“server”asp:DropDownList id=“dlDat

  • 问题内容: 据我了解,字符是单个字符,即字母, 数字 ,标点符号,制表符,空格或类似内容。因此,当我这样做时: 输出1正是我所期望的。那为什么当我这样做时: 我最终的输出是 50 吗? 问题答案: 之所以得到它,是因为它添加了char 的ASCII值。您必须先将其转换为int。

  • 我可以使用iText7和C#ASP. NET创建一个pdf文件 这个pdf文件共包含9页。 我试图在代码中添加总页数和一条虚线 但我有两个问题 页数(9页中的1页,9页中的2页,9页中的3页...)只显示在PDF文件中的第8页和第9页中 虚线没有显示在第一页... 任何帮助都将不胜感激。。。非常感谢。 下面是我的代码

  • 嗨,伙计们,我在做零碎交易时有点困惑。以下是细节。 我有四个片段,A,B,C,D。 我所做的是: 第一个A(替换) A-- B... C-- 现在,当我在片段D中并按下back按钮时,当我按下hardware back键时,导航将跟随这里。 D-- 即使我将片段C添加到后堆栈中,我也不知道它已经到哪里去了 请帮我做这个。 这里是我整个演示项目的链接

  • 我目前正在努力设置cookie/auth令牌的超时时间,以验证我的。NET Core应用程序通过OpenIdConnect身份验证模型使用Azure AD。 正在通过以下方式在ConfigureServices方法中设置登录方案: 然后我设置我的配置如下: 请注意,我没有使用内置标识(因为它对我们的目的不实用),而是使用自定义中间件。 在中间件层中,我检查用户是否经过身份验证,如果没有,则发出质询