OpenID与ASP.NET MVC连接

时间:2018-06-13 13:55:30

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

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

我跟着this post尝试了但没有运气!

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

1 个答案:

答案 0 :(得分:2)

首先,您必须忘记在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;

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

namespace MVC_OWIN_Client
{
  public class Startup
  {
    public void Configuration(IAppBuilder app)
    {
        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

        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",
        });
    }
  }
}

使用&#34; Owin&#34;,&#34; Katana&#34;和#34; Identityserver3&#34;用于进一步搜索的关键字。