The unified authentication mechanism in MVC

Source: Internet
Author: User
Tags valid email address

Using Mvcapplication2.models;
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.ComponentModel.DataAnnotations;
Using System.ComponentModel.DataAnnotations.Schema;
Using System.Data.Entity;
Using System.Globalization;
Using System.Text.RegularExpressions;
Using System.Web.Security;

Namespace Mvcapplication2.models
{
#region Validating base Classes
<summary>
Generic validation base class
</summary>
Public abstract class Entityvalidationattribute:validationattribute
{
#region Constructors
Public Entityvalidationattribute (MessageType messageId, params object[] args):
Base (() = MessageManager.Current.GetMessage (messageId, args)) {}
#endregion

#region Protected Properties
Protected virtual regex Rletters {get {return new Regex ("[A-za-z]{1,}");}}
<summary>
Verifying numbers
Subclasses can rewrite them according to their own logic.
</summary>
Protected virtual regex Rdigit {get {return new Regex ("[0-9]{1,}");}}
<summary>
Verify ZIP Code
Subclasses can rewrite them according to their own logic.
</summary>
Protected virtual regex Rpostnumber {get {return new regex ("^[0-9]{3,14}$");}}
<summary>
Verify your phone
Subclasses can rewrite them according to their own logic.
</summary>
Protected virtual regex Rmobile {get {return new Regex (@ "^1[3|4|5|8][0-9]\d{8}$");}}
<summary>
Verify Phone
Subclasses can rewrite them according to their own logic.
</summary>
Protected virtual regex Rtelephone {get {return new Regex (@ "^[0-9]{2,4}-\d{6,8}$");}}
<summary>
Verifying faxes
Subclasses can rewrite them according to their own logic.
</summary>
Protected virtual regex Rfex {get {return new Regex (@ "/^[0-9]{2,4}-\d{6,8}$");}}
<summary>
Verify Email
Subclasses can rewrite them according to their own logic.
</summary>
Protected virtual regex REmail {get {return new Regex (@ "^ [\w-\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) $"); } }
#endregion

}
#endregion

#region Specific authentication module
//<SUMMARY>
//NULL verification
//</summary>
[AttributeUsage ( Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Requiredattribute:entityvalidationattribute
{
public bool Allowemptystrings {get; set;}
Public RequiredAttribute (MessageType MessageType, params object[] args):
Base (MessageType, args)
{}
Pub LIC override bool IsValid (object value)
{
return new System.ComponentModel.DataAnnotations.RequiredAttribute { Allowemptystrings = this. Allowemptystrings}. IsValid (value);
}
}
//<summary>
//range validation
//</summary>
[AttributeUsage ( Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Rangeattribute:entityvalidationattribute
{
Private System.ComponentModel.DataAnnotations.RangeAttribute Innerrangeattribute;

Public Rangeattribute (double minimum, double maximum, MessageType MessageType, params object[] args):
Base (MessageType, args)
{
Innerrangeattribute = new System.ComponentModel.DataAnnotations.RangeAttribute (minimum, maximum);
}

public rangeattribute (int minimum, int maximum, MessageType MessageType, params object[] args):
Base (MessageType, args)
{
Innerrangeattribute = new System.ComponentModel.DataAnnotations.RangeAttribute (minimum, maximum);
}

Public Rangeattribute (Type type, string minimum, string maximum, MessageType MessageType, params object[] args):
Base (MessageType, args)
{
Innerrangeattribute = new System.ComponentModel.DataAnnotations.RangeAttribute (type, minimum, maximum);
}

public override bool IsValid (object value)
{
return Innerrangeattribute.isvalid (value);
}
}

//<summary>
//email authentication
//</summary>
[AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Emailattribute:entityvalidationattribute
{
Public Emailattribute (MessageType MessageType, params object[] args):
Base (MessageType, args) {}
Public ove rride bool IsValid (object value)
{
if (value = = null)
return false;
Else
return Remail.ismatch (value.t Ostring ());
}
}

<summary>
Numerical validation
</summary>
[AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Digitattribute:entityvalidationattribute
{
Public Digitattribute (MessageType MessageType, params object[] args):
Base (MessageType, args) {}
public override bool IsValid (object value)
{
if (value = = null)
return false;
Else
return Rdigit.ismatch (value. ToString ());
}

}

<summary>
Postcode verification
</summary>
[AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Postnumberattribute:entityvalidationattribute
{
Public Postnumberattribute (MessageType MessageType, params object[] args):
Base (MessageType, args) {}
public override bool IsValid (object value)
{
if (value = = null)
return false;
Else
return Rpostnumber.ismatch (value. ToString ());
}

}

//<summary>
//Mobile authentication
//</summary>
[AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Mobileattribute:entityvalidationattribute
{
Public Mobileattribute (MessageType MessageType, params object[] args):
Base (MessageType, args) {}
Public ov erride bool IsValid (object value)
{
if (value = = null)
return false;
Else
return Rmobile.ismatch (value . ToString ());
}
}

//<summary>
//Telephone authentication
//</summary>
[AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Telephoneattribute:entityvalidationattribute
{
Public telephoneattribute (MessageType MessageType, params object[] args):
Base (MessageType, args) {}
Publi C override bool IsValid (object value)
{
if (value = = null)
return false;
Else
return RTELEPHONE.ISMATC H (value. ToString ());
}
}

<summary>
Fax verification
</summary>
[AttributeUsage (Attributetargets.parameter | Attributetargets.field | Attributetargets.property, AllowMultiple = False)]
public class Fexattribute:entityvalidationattribute
{
Public Fexattribute (MessageType MessageType, params object[] args):
Base (MessageType, args) {}
public override bool IsValid (object value)
{
if (value = = null)
return false;
Else
return Rfex.ismatch (value. ToString ());
}
}
#endregion

#region Validation message return class
<summary>
Message class
</summary>
public class MessageManager
{
Static Dictionary<messagetype, string> messages = new Dictionary<messagetype, string> ();
Static MessageManager ()
{
Messages. ADD (Messagetype.requiredfield, "this \" {0}\ "is required!");
Messages. ADD (Messagetype.greaterthan, "this \" {0}\ "value must be greater than \" {1}\ "!");
Messages. ADD (Messagetype.lessthan, "this \" {0}\ "value must be less than \" {1}\ "!");
Messages. ADD (Messagetype.emailfield, "this \" {0}\ "is not a valid email address!");
Messages. ADD (Messagetype.digitfield, "this \" {0}\ "is not a valid number!");
Messages. ADD (Messagetype.postnumberfield, "this \" {0}\ "is not a valid ZIP code!");
Messages. ADD (Messagetype.mobilefield, "this \" {0}\ "is not a valid mobile phone number!");
Messages. ADD (Messagetype.telephonefield, "this \" {0}\ "is not a valid phone number!");
Messages. ADD (Messagetype.fexfield, "this \" {0}\ "is not a valid fax!");
}
<summary>
A collection of messages that get validation exceptions
Public outreach
</summary>
<param name= "MessageType" > Exception message id</param>
<param name= "args" > Message Parameters Collection </param>
<returns></returns>
public string GetMessage (MessageType MessageType, params object[] args)
{
return string. Format (CultureInfo.CurrentCulture, Messages[messagetype], args);
}
<summary>
instance objects of this class
</summary>
public static MessageManager current = new MessageManager ();
}

#endregion

#region validation Type Enumeration
<summary>
Verify Message type
</summary>
public enum MessageType
{
<summary>
For null validation
</summary>
Requiredfield,
<summary>
Greater than validation
</summary>
GreaterThan,
<summary>
Less than validation
</summary>
LessThan,
<summary>
Mailbox Verification
</summary>
Emailfield,
<summary>
Digital verification
</summary>
Digitfield,
<summary>
Postcode verification
</summary>
Postnumberfield,
<summary>
Mobile Verification
</summary>
Mobilefield,
<summary>
Phone verification
</summary>
Telephonefield,
<summary>
Fax verification
</summary>
Fexfield,
}
#endregion

}

The complete entity is:

<summary>
Human entities
</summary>
public class Person
{
<summary>
Email
</summary>
[DisplayName ("email"), email (messagetype.emailfield, "email")]
public string Email {get; set;}

<summary>
Cell phone
</summary>
[DisplayName ("mobile"), Mobile (Messagetype.mobilefield, "mobile")]
public string Mobile {get; set;}

<summary>
Phone
</summary>
[DisplayName ("Phone"), telephone (Messagetype.telephonefield, "telephone")]
public string Telephone {get; set;}

<summary>
Zip
</summary>
[DisplayName ("Zip Code"), Postnumber (Messagetype.postnumberfield, "Postnumber")]
public string Postnumber {get; set;}

<summary>
Fax
</summary>
[DisplayName ("Fax"), Fex (Messagetype.fexfield, "Fex")]
public string Fex {get; set;}
}
}

The unified authentication mechanism in MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.