Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var clientOptions = new AuthenticatedClientOptions
            {
                AuthorizationServer = new AuthorizationServer(ConfigurationManager.AppSettings["IdSrv"]),
                ClientId            = ConfigurationManager.AppSettings["ClientId"],
                ClientSecret        = ConfigurationManager.AppSettings["ClientSecret"],
                Scope = ConfigurationManager.AppSettings["ApiScope"]
            };

            var factory = new AuthenticatedHttpClientFactory(clientOptions, new Uri(ConfigurationManager.AppSettings["ApiUri"]));

            try
            {
                var client      = factory.GetHttpClient();
                var bearerToken = client.DefaultRequestHeaders.Authorization;

                var response = client.GetAsync("/api/ping").GetAwaiter().GetResult();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Clipboard.SetText(bearerToken.ToString());
                    Console.WriteLine("Authorization header er kopiert til utklipstavlen. Trykk en tast for å lukke dette vinduet");
                }
                else
                {
                    Console.WriteLine("Klarte ikke å hente Bearertoken");
                }
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
 public AuthenticatedHttpClientFactory(AuthenticatedClientOptions options, Uri resourceServerBase, Action <string> debugMessageHandler = null)
 {
     _options             = options;
     _resourceServerBase  = resourceServerBase;
     _debugMessageHandler = debugMessageHandler ?? (s => { });
 }
Ejemplo n.º 3
0
        public void Configuration(IAppBuilder app)
        {
            var clientOptions = new AuthenticatedClientOptions
            {
                AuthorizationServer = new AuthorizationServer(ConfigurationManager.AppSettings["IdSrv"]),
                ClientId            = ConfigurationManager.AppSettings["ClientId"],
                Scope = "openid profile"
            };

            JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = CookieAuthenticationDefaults.AuthenticationType
            });
            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                AuthenticationType         = OpenIdConnectAuthenticationDefaults.AuthenticationType,
                SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
                Authority             = clientOptions.AuthorizationServer.BaseAddress.ToString(),
                ClientId              = clientOptions.ClientId,
                Scope                 = clientOptions.Scope,
                ResponseType          = OidcConstants.TokenTypes.IdentityToken,
                RedirectUri           = "https://localhost:44392",
                PostLogoutRedirectUri = "https://localhost:44392/Home/AfterLogout",
                Notifications         = new OpenIdConnectAuthenticationNotifications
                {
                    RedirectToIdentityProvider = n =>
                    {
                        switch (n.ProtocolMessage.RequestType)
                        {
                        case OpenIdConnectRequestType.LogoutRequest:
                            var idToken = n.OwinContext.Authentication.User.FindFirst(OidcConstants.TokenTypes.IdentityToken);
                            if (idToken != null)
                            {
                                n.ProtocolMessage.IdTokenHint = idToken.Value;
                            }
                            break;

                        case OpenIdConnectRequestType.TokenRequest:
                            break;

                        case OpenIdConnectRequestType.AuthenticationRequest:
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                        return(Task.FromResult(0));
                    },
                    SecurityTokenValidated = n =>
                    {
                        var id    = n.AuthenticationTicket.Identity;
                        var newId = new ClaimsIdentity(id.AuthenticationType, "uid", "role");
                        id.TransferClaims(c => newId.AddClaim(c), "uid", "idp", "trx", "sid");

                        var idToken = n.ProtocolMessage.IdToken;
                        newId.AddClaim(new Claim(OidcConstants.TokenTypes.IdentityToken, idToken));
                        newId.AddClaim(new Claim("logout_protect", Guid.NewGuid().ToString()));

                        n.AuthenticationTicket = new AuthenticationTicket(newId, n.AuthenticationTicket.Properties);
                        return(Task.FromResult(0));
                    }
                }
            });
        }