Last active
December 13, 2016 14:46
-
-
Save ntotten/4627850 to your computer and use it in GitHub Desktop.
Implementing a Simple Registration Code Check in ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to put registration code in the web config sir?