【问题标题】:OpenID Connect with ASP.NET MVCOpenID 与 ASP.NET MVC 连接
【发布时间】:2018-06-13 13:55:29
【问题描述】:

标题中的问题相当简单。 Internet 上的所有教程都在讨论 .NET Core 中的 OpenID Connect 实现。我目前的项目是用 ASP.NET MVC(不是 ASP.NET Core)开发的,我需要在其中实现 OpenID Connect。

我关注this post 并尝试过,但没有成功!

对此的任何帮助/澄清将不胜感激。

【问题讨论】:

  • 您已经知道支持 openID 连接,为了更易于使用的实现,请尝试使用 identityServer 4,youtube 搜索“identityServer4”
  • 谢谢@Saj。即使在 youtube 上,所有视频似乎都与 .NET Core 相关。我想用普通的 .NET 实现它
  • 服务器端的东西在 dotnet core 中要好得多
  • @saj 是的,可能是这样,但我们必须使用我们拥有的东西,而不是我们想要的东西。更改为 .NET Core 并不总是一种选择。
  • 我添加了这个答案,因为我仍然没有“50声望”来评论:看看这个链接:docs.microsoft.com/pt-br/azure/active-directory/develop/…也许它可以帮助你; Obs:要阅读英文文章,请将勾选“Ler em Inglês”设置为 True;

标签: c# asp.net-mvc authentication openid-connect


【解决方案1】:

首先你必须忘记在 web.config 中配置权限。
然后,您必须确保将 Authorize 属性分配给每个控制器(确保使用全局过滤器方法)。
参考 Microsoft.Owin.Security.OpenIdConnect 及其所有依赖项。
使用public void Configuration(IAppBuilder app) 方法添加 Owin Startup 类。如下:

using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
//before v5.0 was: using System.IdentityModel.Tokens;

[assembly: OwinStartup(typeof(MVC_OWIN_Client.Startup))]

namespace MVC_OWIN_Client
{
  public class Startup
  {
    public void Configuration(IAppBuilder app)
    {
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = 
            new Dictionary<string, string>();
        // before v5.0 was: JwtSecurityTokenHandler.InboundClaimTypeMap

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies"
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            ClientId = <Your app instance' identifier, must be registered in IdP>,
            Authority = <your IdP>,
            RedirectUri = <base address of your app as registered in IdP>,
            ResponseType = "id_token",
            Scope = "openid email",

            UseTokenLifetime = false,
            SignInAsAuthenticationType = "Cookies",
        });
    }
  }
}

使用“Owin”、“Katana”和“Identityserver3”关键字进行进一步搜索。

【讨论】:

  • 谢谢@d_F。此代码可以在 .NET 非核心中工作吗?我想在一个用 ASP.NET(不是 ASP.NET Core)编写的现有应用程序中实现它,任何解释此代码的参考资料都会很棒。
  • 使用 Microsoft.Owin;意味着它不是 ASP.NET Core。您可以使用我建议的关键字进行谷歌搜索,并获得许多有用的主题,帮助解决许多相关情况。查找 Identityserver 3 文档,它也可能很有用。而且,通过一步一步地按照我的回答,你已经有了最简单的工作解决方案。
  • 在此处查看 IdentityServer3 文档:identityserver.github.io/Documentation/docsv2/overview/…
  • 使用更高版本的 Microsoft System.IdentityModels.Tokens.Jwt 软件包,您将需要 using System.IdentityModel.Tokens.JwtJwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary&lt;string, string&gt;(); 来源:stackoverflow.com/questions/38080608/…
  • @MyDaftQuestions,它是身份提供者
猜你喜欢
  • 2021-05-24
  • 2018-05-15
  • 2021-10-23
  • 2017-01-30
  • 2019-10-12
  • 1970-01-01
  • 2010-09-25
  • 2018-05-06
  • 2021-09-10
相关资源
最近更新 更多