Skip to content

Instantly share code, notes, and snippets.

@ntotten
Last active December 13, 2016 14:46
Show Gist options
  • Save ntotten/4627850 to your computer and use it in GitHub Desktop.
Save ntotten/4627850 to your computer and use it in GitHub Desktop.
Implementing a Simple Registration Code Check in ASP.NET MVC
<li class="name">
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li class="name">
@Html.LabelFor(m => m.RegistrationCode)
@Html.TextBoxFor(m => m.RegistrationCode)
@Html.ValidationMessageFor(m => m.RegistrationCode)
</li>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.RegistrationCode)
@Html.TextBoxFor(m => m.RegistrationCode)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
public class RegisterExternalLoginModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[ValidRegistrationCode]
[Display(Name = "Registration Code")]
public string RegistrationCode { get; set; }
public string ExternalLoginData { get; set; }
}
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[ValidRegistrationCode]
[Display(Name = "Registration Code")]
public string RegistrationCode { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class ValidRegistrationCodeAttribute : ValidationAttribute
{
public ValidRegistrationCodeAttribute()
{
this.ErrorMessage = "Invalid registration code.";
}
public override bool IsValid(object value)
{
if (value == null) return false;
// In this example, I am storing my registration code in the web.conf file.
// You can store the code however you like including web.config or a database.
var validCode = ConfigurationManager.AppSettings["RegistrationCode"];
return validCode.Equals(value.ToString());
}
}
@bastasecret123
Copy link

bastasecret123 commented Dec 13, 2016

how to put registration code in the web config sir?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment