OpenID与ASP.NET MVC连接

Tej*_*tar 4 c# authentication asp.net-mvc openid-connect

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

我按照这篇文章尝试,但没有成功!

任何帮助/澄清这将不胜感激。

d_f*_*d_f 5

首先,您必须忘记在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",
        });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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