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.