Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Security.Cryptography;
public partial class Default2 : System.Web.UI.Page
{
  GoogleTOTP tf;

    private long lastInterval;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //pictureBox1.Image = tf.GenerateImage(pictureBox1.Width, pictureBox1.Height, txtUserEmail.Text);

        Image1.ImageUrl = tf.GenerateImage(Image1.Width, Image1.Height, txtUserEmail.Text);
        txtOutput.Text = tf.GeneratePin();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        long thisInterval = tf.getCurrentInterval();
        if (lastInterval != thisInterval)
        {
            txtOutput.Text = tf.GeneratePin();
            lastInterval = thisInterval;
        }
    }
}



The type or namespace name 'GoogleTOTP' could not be found (are you missing a using directive or an assembly reference?)	



i have also one class




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Drawing;
using GoogleAuth;
namespace GoogleAuth
{
   public  class GoogleTOTP
	{
		RNGCryptoServiceProvider rnd;
		protected string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

		private int intervalLength;
		private int pinCodeLength;
		private int pinModulo;

		private byte[] randomBytes = new byte[10];

		public GoogleTOTP()
		{
			rnd = new RNGCryptoServiceProvider();

			pinCodeLength = 6;
			intervalLength = 30;
			pinModulo = (int)Math.Pow(10, pinCodeLength);

			rnd.GetBytes(randomBytes);
		}

		public byte[] getPrivateKey()
		{
			return randomBytes;
		}

		/// <summary>
		/// Generates a PIN of desired length when given a challenge (counter)
		/// </summary>
		/// <param name="challenge">Counter to calculate hash</param>
		/// <returns>Desired length PIN</returns>
		private String generateResponseCode(long challenge, byte[] randomBytes)
		{
			HMACSHA1 myHmac = new HMACSHA1(randomBytes);
			myHmac.Initialize();

			byte[] value = BitConverter.GetBytes(challenge);
			Array.Reverse(value); //reverses the challenge array due to differences in c# vs java
			myHmac.ComputeHash(value);
			byte[] hash = myHmac.Hash;
			int offset = hash[hash.Length - 1] & 0xF;
			byte[] SelectedFourBytes = new byte[4];
			//selected bytes are actually reversed due to c# again, thus the weird stuff here
			SelectedFourBytes[0] = hash[offset];
			SelectedFourBytes[1] = hash[offset + 1];
			SelectedFourBytes[2] = hash[offset + 2];
			SelectedFourBytes[3] = hash[offset + 3];
			Array.Reverse(SelectedFourBytes);
			int finalInt = BitConverter.ToInt32(SelectedFourBytes, 0);
			int truncatedHash = finalInt & 0x7FFFFFFF; //remove the most significant bit for interoperability as per HMAC standards
			int pinValue = truncatedHash % pinModulo; //generate 10^d digits where d is the number of digits
			return padOutput(pinValue);
		}

		/// <summary>
		/// Gets current interval number since Unix Epoch based on given interval length
		/// </summary>
		/// <returns>Current interval number</returns>
		public long getCurrentInterval()
		{
			TimeSpan TS = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
			long currentTimeSeconds = (long)Math.Floor(TS.TotalSeconds);
			long currentInterval = currentTimeSeconds / intervalLength; // 30 Seconds
			return currentInterval;
		}

		/// <summary>
		/// Pads the output string with leading zeroes just in case the result is less than the length of desired digits
		/// </summary>
		/// <param name="value">Value to pad</param>
		/// <returns>Padded Result</returns>
		private String padOutput(int value)
		{
			String result = value.ToString();
			for (int i = result.Length; i < pinCodeLength; i++)
			{
				result = "0" + result;
			}
			return result;
		}

		/// <summary>
		/// This is a different Url Encode implementation since the default .NET one outputs the percent encoding in lower case.
		/// While this is not a problem with the percent encoding spec, it is used in upper case throughout OAuth
		/// </summary>
		/// <param name="value">The value to Url encode</param>
		/// <returns>Returns a Url encoded string</returns>
		protected string UrlEncode(string value)
		{
			StringBuilder result = new StringBuilder();

			foreach (char symbol in value)
			{
				if (unreservedChars.IndexOf(symbol) != -1)
				{
					result.Append(symbol);
				}
				else
				{
					result.Append('%' + String.Format("{0:X2}", (int)symbol));
				}
			}

			return result.ToString();
		}

		public Image GenerateImage(int width, int height, string email)
		{
			string randomString = CreativeCommons.Transcoder.Base32Encode(randomBytes);
            string ProvisionUrl = UrlEncode(String.Format("otpauth://totp/me@mywindow.com?secret=xFwkAguLCqLPsyxLowR5BWjU", email, randomString));
			string url = String.Format("http://chart.apis.google.com/chart?cht=qr&chs={0}x{1}&chl={2}", width, height, ProvisionUrl);

			WebClient wc = new WebClient();
			var data = wc.DownloadData(url);

			using (var imageStream = new MemoryStream(data))
			{
				return new Bitmap(imageStream);
			}
		}

		public string GeneratePin()
		{
			return generateResponseCode(getCurrentInterval(), randomBytes);
		}

	}
}
Posted
Comments
[no name] 21-Apr-14 7:45am    
The error message means exactly that. You are not including a "using" statement for GoogleAuth or providing the fully qualified namespace/classname.

1 solution

Your class GoogleTOTP is in the namespace GoogleAuth, but you don;t refer to it directly, or via a using statement.

You could qualify it manually, but the easy way is to put the text cursor in the class name that VS is complaining about (GoogleTOTP in this case) and a blue line will appear at the beginning. Hover the mouse over the blue line, and a drop down will appear.
Open the dropdown, and VS will give you a list of possible solutions, which it can implement for you. These will include adding the qualified name to this instance, and adding the appropriate using statement.
If the blue line doesn't appear, then VS can't find the class at all, and you need to look at your spelling / character case / references.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900