using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Grasshoppergolf.Models;
using GrasshoppergolfModels.DAL;
using Grasshoppergolf.Filters;
using WebMatrix.WebData;
using System.Web.Security;
using System.Configuration;
using System.Text;
using Grasshoppergolf.Common;
namespace Grasshoppergolf.Controllers
{
[InitializeSimpleMembership]
public class UserController : Controller
{
//
// GET: /User/
private IRepositoryUser __db { get; set; }
[AllowAnonymous]
public ActionResult Registration()
{
return View();
}
///
/// User registration
/// Created by: Prince
/// Created date: 10/12/2013 2:53PM
/// Modified date:
///
///
///
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ViewResult Registration(UserModel model)
{
__db = new RepositoryUser();
if (ModelState.IsValid)
{
var Email_exists = __db.CheckEmailExistsForRegister(model.Email);
if (!Email_exists)
{
string password = GetRandomPasswordUsingGUID(6);
StringBuilder _MailBody = new StringBuilder();
_MailBody.Append("Hello " + model.UserName+"," + "
");
_MailBody.Append("Your account has been created. Please use following details to login in.
");
_MailBody.Append("UserName: "+model.Email+"
");
_MailBody.Append("Password: " + password + "
");
_MailBody.Append("Thanks and Regards
GRASSHOPERGOLF TEAM
");
var Status = EmailUtility.SendMail(model.Email, _MailBody.ToString(), "", false, true, null, null, null);
if (Status == "Ok")
{
var token = WebSecurity.CreateUserAndAccount(model.Email, password, null, true);
WebSecurity.Login(model.Email, password);
Roles.AddUserToRole(model.Email.ToString(), "User");
Int32 RegisteredUserId = WebSecurity.GetUserId(model.Email);
var User = __db.CreateUser();
User.UserId = RegisteredUserId;
User.Username = model.Email;
User.Email = model.Email;
User.CreatedDate = DateTime.Now;
User.IsActive = true;
User.ModifiedDate = DateTime.Now;
User.IsAutoGenerated = false;
__db.InsertUser(User);
var UserDetails = __db.CreateUserDetails();
UserDetails.UserId = RegisteredUserId;
UserDetails.FullName = model.UserName;
UserDetails.LanguageId = Convert.ToInt16(ConfigurationManager.AppSettings["LanguageDefault"]);
UserDetails.CurrencyId = Convert.ToInt16(ConfigurationManager.AppSettings["CurrencyDefault"]);
__db.InsertUserDetails(UserDetails);
WebSecurity.ConfirmAccount(token);
model.Email = "";
model.FullName = "";
model.UserName = "";
}
else
{
ModelState.AddModelError("",Messages.ServerError);
}
}
else
{
ModelState.AddModelError("", Messages.EmailAlreadyExists);
}
}
return View(model);
}
public static string GetRandomPasswordUsingGUID(int length)
{
string guidResult = System.Guid.NewGuid().ToString();
guidResult = guidResult.Replace("-", string.Empty);
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
return guidResult.Substring(0, length);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Grasshoppergolf.Models;
using GrasshoppergolfModels.DAL;
using Grasshoppergolf.Filters;
using WebMatrix.WebData;
using System.Web.Security;
using System.Configuration;
using System.Text;
using Grasshoppergolf.Common;
namespace Grasshoppergolf.Controllers
{
[InitializeSimpleMembership]
public class UserController : Controller
{
//
// GET: /User/
private IRepositoryUser __db { get; set; }
[AllowAnonymous]
public ActionResult Registration()
{
return View();
}
///
/// User registration
/// Created by: Prince
/// Created date: 10/12/2013 2:53PM
/// Modified date:
///
///
///
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ViewResult Registration(UserModel model)
{
__db = new RepositoryUser();
if (ModelState.IsValid)
{
var Email_exists = __db.CheckEmailExistsForRegister(model.Email);
if (!Email_exists)
{
string password = GetRandomPasswordUsingGUID(6);
StringBuilder _MailBody = new StringBuilder();
_MailBody.Append("Hello " + model.UserName+"," + "
");
_MailBody.Append("Your account has been created. Please use following details to login in.
");
_MailBody.Append("UserName: "+model.Email+"
");
_MailBody.Append("Password: " + password + "
");
_MailBody.Append("Thanks and Regards
GRASSHOPERGOLF TEAM
");
var Status = EmailUtility.SendMail(model.Email, _MailBody.ToString(), "", false, true, null, null, null);
if (Status == "Ok")
{
var token = WebSecurity.CreateUserAndAccount(model.Email, password, null, true);
WebSecurity.Login(model.Email, password);
Roles.AddUserToRole(model.Email.ToString(), "User");
Int32 RegisteredUserId = WebSecurity.GetUserId(model.Email);
var User = __db.CreateUser();
User.UserId = RegisteredUserId;
User.Username = model.Email;
User.Email = model.Email;
User.CreatedDate = DateTime.Now;
User.IsActive = true;
User.ModifiedDate = DateTime.Now;
User.IsAutoGenerated = false;
__db.InsertUser(User);
var UserDetails = __db.CreateUserDetails();
UserDetails.UserId = RegisteredUserId;
UserDetails.FullName = model.UserName;
UserDetails.LanguageId = Convert.ToInt16(ConfigurationManager.AppSettings["LanguageDefault"]);
UserDetails.CurrencyId = Convert.ToInt16(ConfigurationManager.AppSettings["CurrencyDefault"]);
__db.InsertUserDetails(UserDetails);
WebSecurity.ConfirmAccount(token);
model.Email = "";
model.FullName = "";
model.UserName = "";
}
else
{
ModelState.AddModelError("",Messages.ServerError);
}
}
else
{
ModelState.AddModelError("", Messages.EmailAlreadyExists);
}
}
return View(model);
}
public static string GetRandomPasswordUsingGUID(int length)
{
string guidResult = System.Guid.NewGuid().ToString();
guidResult = guidResult.Replace("-", string.Empty);
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
return guidResult.Substring(0, length);
}
}
}
No comments:
Post a Comment