我正在尝试将一些数据从一个剃须刀页面发送到另一剃须刀页面。对于我的情况,用户正在尝试注册一个帐户。当他们提交信息时,他们会有一个确认码发送到他们的电子邮件中(在这种情况下,“电子邮件”存储在C:/中的文件中),然后当他们返回Web应用程序以输入验证码并进行确认后,该帐户注册并发送到下一页。我无法知道当前在Webapp上运行的用户是什么。因此,在注册页面上,他们输入信息,然后生成代码,然后将它们发送到“确认”页面,当他们到达下一页时,在其中放置代码但我不知道谁的确认代码是正确的当前用户,我需要将刚刚注册的用户ID或实际的确认代码传递给ConfirmationPage?我认为您可以通过打开会话来保存用户信息,但是我只想在他们的帐户完全注册并确认后才开始会话。
我相信您可以在这种情况下使用ViewData,但是当我尝试使用ViewData时,发送到下一页的对象为空,并且没有我需要的信息
注册模型
public class RegisterModel : PageModel
{
readonly DatabaseContext _context;
public IAccountService accountService;
public RegisterModel(IAccountService _accountService, DatabaseContext context)
{
this.accountService = _accountService;
_context = context;
}
[BindProperty]
public VisUser _VisUser { get; set; }
[BindProperty]
public Email mails { get; set; }
[Required(ErrorMessage = "Enter User Password"), DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
// Generate a random password
public string RandomCode()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
// Generate a random string with a given size
public string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
// Generate a random number between two numbers
public int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
public ActionResult OnPost()
{
_VisUser.ConfirmationCode = RandomCode();
var visUser = _VisUser;
if (!ModelState.IsValid)
{
return Page();
}
if(visUser.UserPassword.CompareTo(ConfirmPassword) == 1)
{
visUser.UserId = 0;
var result = _context.Add(visUser);
_context.SaveChanges();
SendEmail();
TempData["ConCode"] = visUser.ConfirmationCode;
return RedirectToPage("/Account/ConfirmationCode");
}
return Page();
}
public async Task SendEmail()
{
using(var smtp = new SmtpClient())
{
mails.To = _VisUser.Email;
mails.From = "danny@Vispires.com";
mails.Subject = "Registration Confirmation Code";
mails.Body = _VisUser.ConfirmationCode;
smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtp.PickupDirectoryLocation = @"c:\temp";
var msg = new MailMessage
{
Body = mails.Body,
Subject = mails.Subject,
From = new MailAddress(mails.From)
};
msg.To.Add(mails.To);
await smtp.SendMailAsync(msg);
}
}
}
确认码模型:
public class ConfirmationCodeModel : PageModel
{
readonly DatabaseContext _context;
public IAccountService accountService;
public ConfirmationCodeModel(IAccountService _accountService, DatabaseContext context)
{
this.accountService = _accountService;
_context = context;
}
public string ConCode { get; set; }
[Required(ErrorMessage = "Enter Confirmation Code")]
public string ConfirmCode { get; set; }
public ActionResult OnPost(string ConfirmCode)
{
if (!ModelState.IsValid)
{
return Page();
}
if (ConCode == ConfirmCode)
{
//change isvalid and start session
return RedirectToPage("/CreatePortfolio");
}
else
{
return Page();
}
}
}
验证码视图:
@page
@using Visportfolio.Models @*namespace*@
@model Visportfolio.Pages.Account.ConfirmationCodeModel
@{
ViewData["Title"] = "ConfirmationCode";
}
<h1>Code Authentication</h1>
<h3>Enter Code Here:</h3>
@{
if (TempData["ConCode"] != null)
{
var ConCode = TempData["VisUser"] as string;
}
}
<form method="post">
<div class="row">
<div class="col-md-4">
<label asp-for="ConfirmCode"></label>
<input asp-for="ConfirmCode" class="form-control" />
<span class="alert-danger" asp-validation-for="ConfirmCode"></span>
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary" />
</form>
在RegisterModel> OnPost方法的末尾,我创建的ViewData确实包含确认代码。然后,我尝试在确认代码视图的顶部接收它。然后,当我尝试在该页面的OnPost中访问它时,确认代码为null。
答案 0 :(得分:4)
在您的方案中,您想要注册用户并将确认代码发送到其电子邮件或电话,将确认代码存储在ViewData,Session,ViewBag,TempData中或在剃须刀页面之间进行传输不是一个好主意。如果您的用户关闭其浏览器窗口,则将丢失注册代码。您应使用特定键(如UserId,UserEmail或UserPhoneNumber)将此代码存储在Distributed Cache或Memory Cache中60秒或更长时间。您也可以将此代码存储在数据库中,但是将其存储在缓存中是更好的方法。