public static UsernameToken GetUsernameToken(string username, string password, PasswordOption passwordOption)
		{
			var token = new UsernameToken(username, password, passwordOption);

			var securityTokenManager = SecurityTokenManager.GetSecurityTokenManagerByTokenType(WSTrust.TokenTypes.UsernameToken);
			securityTokenManager.CacheSecurityToken(token);

			return token;
		}
		public UsernameToken(string username, string password, 
			PasswordOption passType, EncodingType encType)
		{
			this.Username = new Username();
			this.Username.Text = username;

			System.Guid g = GuidEx.NewGuid();
			this.Id = "SecurityToken-" + g.ToString("D");

			if(passType == PasswordOption.SendNone)
			{
				this.Password = null;
				DateTime dtCreated = DateTime.UtcNow;
				this.Created = XmlConvert.ToString(dtCreated, "yyyy-MM-ddTHH:mm:ssZ");

				this.Nonce = new Nonce();
				byte [] baNonce = OpenNETCF.Security.Cryptography.NativeMethods.Rand.GetRandomBytes(20);
				this.Nonce.Text = Convert.ToBase64String(baNonce, 0, baNonce.Length);
				return;
			}

			this.Password = new Password();

			if(passType == PasswordOption.SendPlainText)
			{
				//this.Password.Type = "wsse:PasswordText";
				this.Password.Type = Misc.tokenProfUsername + "#PasswordText";
				this.Password.Text = password;
			}

			if(passType == PasswordOption.SendHashed)
			{
				//this.Password.Type = "wsse:PasswordDigest";
				this.Password.Type = Misc.tokenProfUsername + "#PasswordDigest";

				DateTime dtCreated = DateTime.UtcNow;
				this.Created = XmlConvert.ToString(dtCreated, "yyyy-MM-ddTHH:mm:ssZ");
				byte [] baCreated = Encoding.UTF8.GetBytes(this.Created);

				this.Nonce = new Nonce();
				//this random number gen is not as strong
				//Random r = new Random(Environment.TickCount);
				//byte [] baNonce = new byte[20];
				//r.NextBytes(baNonce);
				byte [] baNonce = OpenNETCF.Security.Cryptography.NativeMethods.Rand.GetRandomBytes(20);
				this.Nonce.Text = Convert.ToBase64String(baNonce, 0, baNonce.Length);

				byte [] baPassword = Encoding.UTF8.GetBytes(password);

				int baDigestLength = baNonce.Length + baCreated.Length + baPassword.Length; 
				byte [] baDigest = new byte[baDigestLength]; 
				Array.Copy(baNonce, 0, baDigest, 0, baNonce.Length); 
				Array.Copy(baCreated, 0, baDigest, baNonce.Length, baCreated.Length); 
				Array.Copy(baPassword, 0, baDigest, baNonce.Length + baCreated.Length, baPassword.Length);
 				
				//byte [] hash = Hash.ComputeHash(CalgHash.SHA1, baDigest);
				SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
				byte [] hash = sha1.ComputeHash(baDigest);

				if(encType == EncodingType.Base64Binary)
				{
					//default is Base64Binary so dont have to set
					//this.Password.Type = "wsse:Base64Binary";
					this.Password.Text = Convert.ToBase64String(hash, 0, hash.Length);
				}
				if(encType == EncodingType.HexBinary)
				{
					//this.Password.Type = "wsse:HexBinary";
					this.Password.Type = Misc.tokenProfUsername + "#HexBinary";
					this.Password.Text = OpenNETCF.Security.Cryptography.NativeMethods.Format.GetHexBin(hash);
				}
			}
		}
Example #3
0
 public UsernameTokenSoapAssertion(string username, string password, PasswordOption passwordmode)
 {
     token = new UsernameToken(username, password, passwordmode);
 }
        public UsernameTokenV1(string username, string password,
                               PasswordOption passType, EncodingType encType)
        {
            this.Username      = new Username();
            this.Username.Text = username;

            System.Guid g = GuidEx.NewGuid();
            this.Id = "SecurityToken-" + g.ToString("D");

            if (passType == PasswordOption.SendNone)
            {
                this.Password = null;
            }

            this.Password = new PasswordV1();

            if (passType == PasswordOption.SendPlainText)
            {
                this.Password.Type = "wsse:PasswordText";
                this.Password.Text = password;
            }

            if (passType == PasswordOption.SendHashed)
            {
                this.Password.Type = "wsse:PasswordDigest";

                DateTime dtCreated = DateTime.UtcNow;
                this.Created = XmlConvert.ToString(dtCreated, "yyyy-MM-ddTHH:mm:ssZ");
                byte [] baCreated = Encoding.UTF8.GetBytes(this.Created);

                this.Nonce = new Nonce();
                //this random number gen is not as strong
                //Random r = new Random(Environment.TickCount);
                //byte [] baNonce = new byte[20];
                //r.NextBytes(baNonce);
                byte [] baNonce = OpenNETCF.Security.Cryptography.NativeMethods.Rand.GetRandomBytes(20);
                this.Nonce.Text = Convert.ToBase64String(baNonce, 0, baNonce.Length);

                byte [] baPassword = Encoding.UTF8.GetBytes(password);

                int     baDigestLength = baNonce.Length + baCreated.Length + baPassword.Length;
                byte [] baDigest       = new byte[baDigestLength];
                Array.Copy(baNonce, 0, baDigest, 0, baNonce.Length);
                Array.Copy(baCreated, 0, baDigest, baNonce.Length, baCreated.Length);
                Array.Copy(baPassword, 0, baDigest, baNonce.Length + baCreated.Length, baPassword.Length);
                //byte [] hash = Hash.ComputeHash(CalgHash.SHA1, baDigest);
                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                byte [] hash = sha1.ComputeHash(baDigest);

                if (encType == EncodingType.Base64Binary)
                {
                    //default is Base64Binary so dont have to set
                    //this.Password.Type = "wsse:Base64Binary";
                    this.Password.Text = Convert.ToBase64String(hash, 0, hash.Length);
                }
                if (encType == EncodingType.HexBinary)
                {
                    this.Password.Type = "wsse:HexBinary";
                    this.Password.Text = OpenNETCF.Security.Cryptography.NativeMethods.Format.GetHexBin(hash);
                }
            }
        }
 public UsernameTokenBP10(String username, String password, PasswordOption passwordOption)
     : base(username, password, passwordOption)
 {}
 public void ParseAndValidateInput()
 {
     AccountFile = AccountFileOption.TryParseRequiredString(HasInputErrors);
     Password    = PasswordOption.TryParseRequiredString(HasInputErrors);
 }
        public static UsernameToken GetUsernameToken(string username, string password, PasswordOption passwordOption)
        {
            var token = new UsernameToken(username, password, passwordOption);

            var securityTokenManager = SecurityTokenManager.GetSecurityTokenManagerByTokenType(WSTrust.TokenTypes.UsernameToken);

            securityTokenManager.CacheSecurityToken(token);

            return(token);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PasswordService" /> class.
 /// </summary>
 /// <param name="contrasenaOpcion">The options.</param>
 public PasswordService(IOptions <PasswordOption> contrasenaOpcion)
 {
     _passwordOption = contrasenaOpcion.Value;
 }