I'm pretty new to asp.net mvc 3 but not new to the MVC pattern. I'm having difficult making sense of or finding clear information on customizing the membership provoider using forms authenticiation. I want to add support for email validation and additional
fields (at the mim.).
Part of the problem is that I'm unfamiliar with repository and/or unit of work, part of it is that I just can't find an up-to-date resource. A lot of conversations about it just skip over vital details. If I am looking at a resource that would lead me to
the answers I need, I wouldn't know it.
I've walked through many of the the tutorials, I've been searching around for about a week now. I've tried finding pre-built solutions but am finding it difficult to get them working.
If any one could point me in the right direction for instructions, I would really appriciate it.
As to email validaion you need to decorate the field in your model with validation attributes. The example below also uses remote validation, which is connected to an action method which checks wether the email already exists in the database:
[Required]
[DataType(DataType.EmailAddress)]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please enter a valid email address")]
[StringLength(150, ErrorMessage = "Email address may not be longer than 150 characters")]
[Remote("DisallowDuplicateEmail", "User")]
[Display(Name = "Email address")]
public string Email { get; set; }
public ActionResult DisallowDuplicateEmail(string email)
{
try
{
using (var WS = new HLServiceClient())
{
if (!WS.IsEmailInUse(email))
{
return Json(true, JsonRequestBehavior.AllowGet);
}
return Json(string.Format("Error! Email: \"{0}\" is already registered in our database!", email), JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
return Json("Oops! There was a problem: " + ex.Message, JsonRequestBehavior.AllowGet);
}
}
Since you suggested it I've been trying to follow along and I'm unclear on how to simplify it. Here's what I've been trying, please let me know if you think I'm on the right track..I feel like I've taken some licences that I should have (feel free to be mega critical):
public override MembershipUser GetUser(string username, bool userIsOnline)
{
CustomMembershipConext db = new CustomMembershipConext();
var applicationName = new ApplicationId().Name;
User user = db.User.Where(u => u.UserName == username).Where(u => u.ApplicationName == applicationName).FirstOrDefault();
if (userIsOnline)
{
user.LastActivityDate = DateTime.Now;
}
return user;
}
Hi rune007, Thanks for the effort on this! What I actually meant to say was email verification (not validation). In other words, when a new users regististered they are not allowed access until they click a link in an email sent to them from the application
to verify that their email is valid.
I did find this bit in a github project (https://github.com/lthomaz/ASP.NET-MVC-Custom-Authentication/blob/master/CustomAuthentication/Infrastructure/Authentication/DBAuthRepository.cs):
private static void SendActivationEmail(User user)
{
string ActivationLink = "http://localhost:49514/Account/Activate/" +
user.Username + "/" + user.NewEmailKey;
var message = new MailMessage("luiztajr@gmail.com", user.Email)
{
Subject = "Activate your account",
Body = ActivationLink
};
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new System.Net.NetworkCredential("", ""),
EnableSsl = true
};
client.Send(message);
Marked as answer by Ming Xu - MSFT on Feb 28, 2012 12:19 PM
skhot
Member
19 Points
6 Posts
Customize Membership Provider
Feb 12, 2012 01:40 AM|LINK
Hi all,
I'm pretty new to asp.net mvc 3 but not new to the MVC pattern. I'm having difficult making sense of or finding clear information on customizing the membership provoider using forms authenticiation. I want to add support for email validation and additional fields (at the mim.).
Part of the problem is that I'm unfamiliar with repository and/or unit of work, part of it is that I just can't find an up-to-date resource. A lot of conversations about it just skip over vital details. If I am looking at a resource that would lead me to the answers I need, I wouldn't know it.
I've walked through many of the the tutorials, I've been searching around for about a week now. I've tried finding pre-built solutions but am finding it difficult to get them working.
If any one could point me in the right direction for instructions, I would really appriciate it.
I'm using c#, asp.net 4, mvc 3.
Careed
All-Star
18764 Points
3637 Posts
Re: Customize Membership Provider
Feb 12, 2012 06:20 AM|LINK
Have you looked at this:
http://msdn.microsoft.com/en-us/library/ms366730.aspx
"The oxen are slow, but the earth is patient."
ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: Customize Membership Provider
Feb 12, 2012 06:32 AM|LINK
Please be sure that you follow all tutorials from http://www.asp.net/mvc
Please see my tutorial here
http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
rune007
Member
411 Points
186 Posts
Re: Customize Membership Provider
Feb 12, 2012 09:52 AM|LINK
This article helped me implement a custom MembershipProvider:
http://www.c-sharpcorner.com/uploadfile/2124ae/custom-membership-provider-with-form-authentication-in-Asp-Net-mvc-application/
As to email validaion you need to decorate the field in your model with validation attributes. The example below also uses remote validation, which is connected to an action method which checks wether the email already exists in the database:
[Required] [DataType(DataType.EmailAddress)] [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please enter a valid email address")] [StringLength(150, ErrorMessage = "Email address may not be longer than 150 characters")] [Remote("DisallowDuplicateEmail", "User")] [Display(Name = "Email address")] public string Email { get; set; } public ActionResult DisallowDuplicateEmail(string email) { try { using (var WS = new HLServiceClient()) { if (!WS.IsEmailInUse(email)) { return Json(true, JsonRequestBehavior.AllowGet); } return Json(string.Format("Error! Email: \"{0}\" is already registered in our database!", email), JsonRequestBehavior.AllowGet); } } catch (Exception ex) { return Json("Oops! There was a problem: " + ex.Message, JsonRequestBehavior.AllowGet); } }skhot
Member
19 Points
6 Posts
Re: Customize Membership Provider
Feb 13, 2012 11:11 PM|LINK
Hi Careed,
I did look at this but was concerned about these bits with Odbc:
OdbcConnection conn = new OdbcConnection(connectionString); OdbcCommand cmd = new OdbcCommand("SELECT PKID, Username, Email, PasswordQuestion," + " Comment, IsApproved, IsLockedOut, CreationDate, LastLoginDate," + " LastActivityDate, LastPasswordChangedDate, LastLockedOutDate," + " IsSubscriber, CustomerID" + " FROM Users WHERE Username = ? AND ApplicationName = ?", conn); cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username; cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = pApplicationName; OdbcMembershipUser u = null; OdbcDataReader reader = null;Since you suggested it I've been trying to follow along and I'm unclear on how to simplify it. Here's what I've been trying, please let me know if you think I'm on the right track..I feel like I've taken some licences that I should have (feel free to be mega critical):
public override MembershipUser GetUser(string username, bool userIsOnline) { CustomMembershipConext db = new CustomMembershipConext(); var applicationName = new ApplicationId().Name; User user = db.User.Where(u => u.UserName == username).Where(u => u.ApplicationName == applicationName).FirstOrDefault(); if (userIsOnline) { user.LastActivityDate = DateTime.Now; } return user; }Thanks!
skhot
Member
19 Points
6 Posts
Re: Customize Membership Provider
Feb 13, 2012 11:39 PM|LINK
Hi rune007, Thanks for the effort on this! What I actually meant to say was email verification (not validation). In other words, when a new users regististered they are not allowed access until they click a link in an email sent to them from the application to verify that their email is valid.
skhot
Member
19 Points
6 Posts
Re: Customize Membership Provider
Feb 13, 2012 11:40 PM|LINK
I did find this bit in a github project (https://github.com/lthomaz/ASP.NET-MVC-Custom-Authentication/blob/master/CustomAuthentication/Infrastructure/Authentication/DBAuthRepository.cs):
private static void SendActivationEmail(User user) { string ActivationLink = "http://localhost:49514/Account/Activate/" + user.Username + "/" + user.NewEmailKey; var message = new MailMessage("luiztajr@gmail.com", user.Email) { Subject = "Activate your account", Body = ActivationLink }; var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new System.Net.NetworkCredential("", ""), EnableSsl = true }; client.Send(message);ignatandrei
All-Star
134960 Points
21632 Posts
Moderator
MVP
Re: Customize Membership Provider
Feb 14, 2012 06:31 AM|LINK
Wow! You can have an System.Web.HttpContext.Current.Request.RawUrl and parse with URI class.