OpenID Connect with ASP.NET MVC

前端 未结 1 769
無奈伤痛
無奈伤痛 2021-02-04 20:23

The question in the title is fairly simple. All the tutorials available on internet talk about OpenID Connect implementation in .NET Core. My current project is developed in ASP

相关标签:
1条回答
  • 2021-02-04 20:58

    First of all you have to forget about configuring authority in web.config.
    Then you have to ensure you assign Authorize attribute to every controller (use global filter approach to be sure).
    Reference Microsoft.Owin.Security.OpenIdConnect and all its dependencies.
    Add Owin Startup class with public void Configuration(IAppBuilder app) method. As the following:

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

    Use "Owin", "Katana" and "Identityserver3" keywords for further search.

    0 讨论(0)
提交回复
热议问题