public async Task SetCredentials(string userName, string password)
        {
            UserName = userName;
            //// Returns client key & public key
            EncryptPasswordResponse results = await authApi.RegisterAndGetPublicKey();

            //// Encrypt the password
            RsaEncryptionService encryptionService = new RsaEncryptionService {
                PublicKey = results.PublicKey
            };

            EncryptedPassword = encryptionService.EncryptString(password);

            ClientKey = results.ClientKey;
            PublicKey = results.PublicKey;

            //// Store client key, encrypted pw & public key
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            configuration.AppSettings.Settings["ClientKey"].Value         = results.ClientKey;
            configuration.AppSettings.Settings["PublicKey"].Value         = results.PublicKey;
            configuration.AppSettings.Settings["EncryptedPassword"].Value = EncryptedPassword;
            configuration.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("appSettings");
        }
        static void Main(string[] args)
        {
            RsaEncryptionService encryptionService = new RsaEncryptionService();
            string message;

            Console.WriteLine("RSA Public Key");
            Console.WriteLine($"\nExponent: {Convert.ToBase64String(encryptionService.RsaEncryption.PublicRsaParameters.Exponent)}");
            Console.WriteLine($"\nModulus: {Convert.ToBase64String(encryptionService.RsaEncryption.PublicRsaParameters.Modulus)}");

            Console.Write($"\nInput Encrypted Message: ");
            message = Console.ReadLine();

            Console.WriteLine($"\nDecrypted Message: {encryptionService.Decrypt(message)}");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            RsaEncryptionService encryptionService = new RsaEncryptionService();
            string exponent;
            string modulus;
            string message;

            Console.Write($"Input Exponent: ");
            exponent = Console.ReadLine();

            Console.Write($"\nInput Modulus: ");
            modulus = Console.ReadLine();

            Console.Write($"\nInput Message: ");
            message = Console.ReadLine();

            Console.WriteLine($"\nEncrypted Message: {encryptionService.Encrypt(message, exponent, modulus)}");
            Console.ReadLine();
        }
Example #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();

            if (!_env.IsDevelopment())
            {
                services.Configure <MvcOptions>(options => { options.Filters.Add(new RequireHttpsAttribute()); });
            }

            //string appName = Configuration["AppSettings:AppName"];

            services.AddOptions();

            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });

            AppSettings appSettings = new AppSettings();

            Configuration.GetSection("AppSettings").Bind(appSettings);

            string thumbprint = Configuration["CertThumb"];

            if (!string.IsNullOrEmpty(appSettings.DataProtectionKeyLocation) && !string.IsNullOrEmpty(thumbprint))
            {
                var cert = RsaEncryptionService.ResolveCertificate(thumbprint);
                services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(appSettings.DataProtectionKeyLocation)).ProtectKeysWithCertificate(cert);
            }

            services.AddSingleton(appSettings);
            services.AddHttpClient();
            services.AddScoped <OAuthClient>();

            if (appSettings.IsUsingOpenIdConnect())
            {
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
                services.AddAuthentication(options =>
                {
                    options.DefaultScheme          = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                })
                .AddCookie()
                .AddCustomOpenIdConnect(o =>
                {
                    o.MetadataAddress      = appSettings.MetadataAddress;
                    o.ClientId             = appSettings.ClientId;
                    o.ClientSecret         = appSettings.ClientSecret;
                    o.ResponseType         = OpenIdConnectResponseType.Code;
                    o.SignInScheme         = CookieAuthenticationDefaults.AuthenticationScheme;
                    o.RequireHttpsMetadata = true;
                    o.Scope.Add("openid");
                    o.Scope.Add("profile");
                    o.Scope.Add("roles");
                    o.CallbackPath = new PathString("/signin");
                    o.SaveTokens   = true;
                    o.TokenValidationParameters = new TokenValidationParameters
                    {
                        NameClaimType = JwtClaimTypes.Subject,
                        RoleClaimType = JwtClaimTypes.Role
                    };
                });
            }
        }