C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,221 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,693 people online right now. Registration is fast and FREE... Join Now!




validation code

 

validation code, Can you get me a secure online validation code for c# app

deepakgaba

6 Mar, 2009 - 06:03 AM
Post #1

New D.I.C Head
*

Joined: 6 Mar, 2009
Posts: 1

Hi guys,

can anyone get me a secure online validation code for one .net c# application that we are developing.

Thanks

User is offlineProfile CardPM
+Quote Post


coden4fun

RE: Validation Code

6 Mar, 2009 - 07:19 AM
Post #2

D.I.C Regular
Group Icon

Joined: 27 Feb, 2009
Posts: 289



Thanked: 6 times
Dream Kudos: 350
My Contributions
validation? What kind of validation would you like?

Here are what could be acceptable validations that I would use depending on what you were doing, and how you wanted to integrate.

CODE

//let's say we have a list of strings
public static List<string> validations = new List<string> {"adfssssss","dfasssssss","dafsssssss","adfsssssss","adsffffff","adsffffff","adfasdfsadfsdf","ddsdsdfasdfsd"};

//forgive me for the random strings, but these could be loaded from a db, hardcoded in there etc...

//next let's do an MD5 Hash function
#region [MD5 Hash Code]
      
        private void btnValidate_Click(object sender, EventArgs e)
        {
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] input =     System.Text.Encoding.ASCII.GetBytes(txtSerial.Text.ToString());
            byte[] hash = md5.ComputeHash(input);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
                sb.Append(hash[i].ToString("X2"));

            txtActivate.Text = sb.ToString();
            
            //see if validation is successful.
           foreach(stirng s in validations)
                if(s == sb.ToString()) MessageBox.Show("Validation Successful", "Validation Sucessful", MessageBoxButton.OK, MessageBoxImage.Information);
               else
                  MessageBox.Show("error", "error", MessagBox.OK, MessageBoxImage.Error);

        }
#endregion


That's one possible way you could validate something, but there are plenty of ways to validate people.

FormsAuthentication

CODE

protected void btnLogin_Click(object sender, EventArgs e)
    {
        AdminUser objAdminUser = new AdminUser();
        objAdminUser.UserName =txtUserName.Text.Trim();
        objAdminUser.Password = txtPassword.Text.Trim();
        if (objAdminUser.AdminLoginUserValidate() != -100)
        {
            if (objAdminUser.UserID > 0)
            {
                // Create the authentication ticket
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                    objAdminUser.UserID.ToString(),  // user id
                    DateTime.Now,               // creation
                    DateTime.Now.AddMinutes(60),// Expiration
                    false,                        // Persistent
                    objAdminUser.UserName + "," + objAdminUser.FirstName);    // UserName and Fill Name
                // Encrypt the ticket.
                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                // Create a cookie and add the encrypted ticket to the cookie as data.
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                // Add the cookie to the outgoing cookies collection.
                Response.Cookies.Add(authCookie);
                //if there is a previous url to redirect them to
                FormsAuthentication.RedirectFromLoginPage(objAdminUser.UserID.ToString(), false);
                //if (Request.QueryString["ReturnUrl"] == null)
                //{
                //    Response.Redirect("Welcome.aspx");
                //}
                //else
                //{
                //    Response.Redirect(Request.QueryString["ReturnUrl"].ToString());
                //}
            }
        }
        else
        {
            lblMsg.Text = "Invalid username and password.";
            ModalPopupExtender2.Show();
        }
    }


In Web.congig

<authentication mode="Forms">
            <forms name="SMGR" loginUrl="login.aspx"  defaultUrl="welcome.aspx" timeout="1" protection="All" path="/"/>
        </authentication>


Impliment in page:

bool IsAttribute = false;
    public void CheckAuthentication()
    {
        if (!Context.User.Identity.IsAuthenticated)
        {
            string strRedirectURL = Server.UrlEncode(Context.Request.ServerVariables["URL"]);
            Context.Response.Redirect("Login.aspx?ReturnUrl=" + strRedirectURL);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAuthentication();
        

    }


There's 2 ways you could validate someone in C#. I personally like the MD5, and possibly a combination of both.
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades

RE: Validation Code

6 Mar, 2009 - 07:27 AM
Post #3

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,324



Thanked: 454 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:37PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month