public static IAppBuilder UseIdentityServerViewLocalization(this IAppBuilder app, 
            IdentityServerOptions identityServerOptions, 
            IdentityServerViewLocalizationOptions options)
        {
            if (identityServerOptions == null) throw new ArgumentNullException("identityServerOptions");
            if (options == null) throw new ArgumentNullException("options");

            var factory = identityServerOptions.Factory;
            if (factory == null) throw new ArgumentNullException("factory");
            
            app.ValidateOptions(identityServerOptions, options);

            OptionsState.Current.RegisterConfiguration(identityServerOptions, options);

            DependenciesConfig.Configure(identityServerOptions, options);

            factory.ViewService = new Registration<IViewService>(typeof(LocaleViewServices));

            app.ConfigureIdentityServerBaseUrl(identityServerOptions.PublicOrigin);

            app.UseEmbeddedResourceFile(
                        "wwwroot/",
                        "/fonts",
                        "/content",
                        "/scripts"
                        );

            var httpConfig = WebApiConfig.Configure(identityServerOptions);
            app.UseWebApi(httpConfig);

            return app;
        }
        public void RegisterConfiguration(IdentityServerOptions identityServerOptions, IdentityServerViewLocalizationOptions options)
        {
            if (identityServerOptions == null) throw new ArgumentNullException("identityServerOptions");
            if (options == null) throw new ArgumentNullException("options");
            if (IdentityServerOptions != null) throw new InvalidOperationException("Options are already registered");

            IdentityServerOptions = identityServerOptions;
            Options = options;
        }
        public static void ValidateOptions(this IAppBuilder app, 
            IdentityServerOptions identityServerOptions, 
            IdentityServerViewLocalizationOptions options)
        {
            if (identityServerOptions == null) throw new ArgumentNullException("identityServerOptions");
            if (options == null) throw new ArgumentNullException("options");

            var defaultLanguage = options.LocalizationServiceOptions.DefaultLanguage;
            if (!VLConstants.Localization.AvailableLanguages.HasLanguage(defaultLanguage))
            {
                throw new ApplicationException(string.Format("DefaultLanguage '{0}' unavailable.", defaultLanguage));
            }

        }
        public void Configuration(IAppBuilder app)
        {
            LogProvider.SetCurrentLogProvider(new DiagnosticsTraceLogProvider());

            app.Map(IdsrvPath, coreApp =>
            {
                var factory = InMemoryFactory.Create(
                    users: Users.Get(),
                    clients: Clients.Get(),
                    scopes: Scopes.Get());

                var options = new IdentityServerOptions
                {
                    IssuerUri = "https://idsrv3.com",
                    SiteName = "IdentityServer3 - LocalizedViewService",
                    RequireSsl = false,
                    EnableWelcomePage = true,
                    SigningCertificate = Certificate.Get(),
                    Factory = factory,
                    CorsPolicy = CorsPolicy.AllowAll,
                    AuthenticationOptions = new AuthenticationOptions
                    {
                        IdentityProviders = ConfigureAdditionalIdentityProviders,
                    }
                };

                var viewLocalizationOptions = new IdentityServerViewLocalizationOptions
                {
                    LocalizationServiceOptions =
                    {
                        // Add not standard scopes
                        ScopeLocalizationRequest = info =>
                        {
                            return new ScopeLocalizationCollection
                            {
                                {
                                    "pt-BR", new ScopeTranslations
                                    {
                                        {"read", "Ler dados", "Ler dados do aplicativo"},
                                        // {"write", "Escrever dados" } // Fallback use default
                                    }
                                },
                                // override defaults
                                {
                                    "en", new ScopeTranslations
                                    {
                                        {"read", "Read application data"},
                                        {"write", "Write application data", "Custom description of Write data"}
                                    }
                                }
                            };
                        }
                    }
                };


                // This "Use" order is important
                coreApp.UseIdentityServerViewLocalization(options, viewLocalizationOptions);
                coreApp.UseIdentityServer(options);
            });

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

            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ClientId = "owin",
                Authority = AuthorityUri,
                RedirectUri = SelfHostUri,
                PostLogoutRedirectUri = SelfHostUri,
                SignInAsAuthenticationType = "Cookies",
                ResponseType = "code id_token token",
                Scope = "openid profile email offline_access phone roles all_claims address",
                // Scope = "openid profile offline_access read write",
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthorizationCodeReceived = async n =>
                    {
                        // filter "protocol" claims
                        var claims = new List<Claim>(from c in n.AuthenticationTicket.Identity.Claims
                                                     where c.Type != "iss" &&
                                                           c.Type != "aud" &&
                                                           c.Type != "nbf" &&
                                                           c.Type != "exp" &&
                                                           c.Type != "iat" &&
                                                           c.Type != "nonce" &&
                                                           c.Type != "c_hash" &&
                                                           c.Type != "at_hash"
                                                     select c);

                        // get userinfo data
                        var userInfoClient = new UserInfoClient(
                            new Uri(UserInfoClientUri),
                            n.ProtocolMessage.AccessToken);

                        var userInfo = await userInfoClient.GetAsync();
                        userInfo.Claims.ToList().ForEach(ui => claims.Add(new Claim(ui.Item1, ui.Item2)));

                        // get access and refresh token
                        var tokenClient = new OAuth2Client(
                            new Uri(TokenClientUri),
                            "owin",
                            "secret");

                        var response = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri);

                        claims.Add(new Claim("access_token", response.AccessToken));
                        claims.Add(new Claim("expires_at", DateTime.Now.AddSeconds(response.ExpiresIn).ToLocalTime().ToString()));
                        claims.Add(new Claim("refresh_token", response.RefreshToken));
                        claims.Add(new Claim("id_token", n.ProtocolMessage.IdToken));

                        n.AuthenticationTicket = new AuthenticationTicket(new ClaimsIdentity(claims.Distinct(new ClaimComparer()), n.AuthenticationTicket.Identity.AuthenticationType), n.AuthenticationTicket.Properties);
                    },
                    RedirectToIdentityProvider = async n =>
                    {
                        // if signing out, add the id_token_hint
                        if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
                        {
                            var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
                            n.ProtocolMessage.IdTokenHint = idTokenHint;
                        }
                    }
                }

            });

            var httpConfig = new HttpConfiguration();

            httpConfig.MapHttpAttributeRoutes();
            httpConfig.Routes.MapHttpRoute("Home", "", new { controller = "Page", action = "Index" });

            httpConfig.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            app.UseWebApi(httpConfig);
        }
 public static void Configure(IdentityServerOptions identityServerOptions,
     IdentityServerViewLocalizationOptions options)
 {
     identityServerOptions.Factory.Register(new Registration<IdentityServerViewLocalizationOptions>(options));
 }
 public LocaleViewServices(OwinEnvironmentService owin, IdentityServerViewLocalizationOptions options)
 {
     _context = new OwinContext(owin.Environment);
     _config = options.ViewServiceOptions;
 }