我正在使用MVC开发我的项目。在注册页面我使用验证码控制。它工作正常。但我的问题是我正在为我的某些页面添加屏幕限制。虽然添加屏幕限制验证码没有加载。 有人可以帮帮我吗?
我在
下添加了屏幕限制代码 public class ScreenRestriction : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var routeData = filterContext.HttpContext.Request.RequestContext.RouteData;
string currentAction = routeData.GetRequiredString("action");
string currentController = routeData.GetRequiredString("controller");
string currentArea = routeData.Values["area"] as string;
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;
var session = filterContext.HttpContext.Session;
if ((currentAction != "Login" && currentAction != "Create" && currentAction != "GetUser" && currentAction != "Index" ) == true)
{
if (session["UserID"] == null)
{
if (request.IsAjaxRequest())
{
response.StatusCode = 590;
}
else
{
var url = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
response.Redirect(url.Action("Login", "CU"));
}
filterContext.Result = new EmptyResult();
}
else
{
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
}
}
}
base.OnActionExecuting(filterContext);
}
过滤器中的屏幕限制
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new ScreenRestriction());
}
}
我已在下面添加了注册页码
查看 -
@model SYTMain.Models.tblUser
@using CaptchaMvc.HtmlHelpers
@using CaptchaMvc;
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout_for_registration.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>tblUser</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MiddleName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MiddleName)
@Html.ValidationMessageFor(model => model.MiddleName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailID)
@Html.ValidationMessageFor(model => model.EmailID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddressId, "Address1")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.tblAddress.Address1)
@Html.ValidationMessageFor(model => model.tblAddress.Address1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddressId, "Address2")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.tblAddress.Address2)
@Html.ValidationMessageFor(model => model.tblAddress.Address2)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MobileNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MobileNumber)
@Html.ValidationMessageFor(model => model.MobileNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.WorkNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.WorkNumber)
@Html.ValidationMessageFor(model => model.WorkNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.HomeNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HomeNumber)
@Html.ValidationMessageFor(model => model.HomeNumber)
</div>
@Html.Captcha(3)
<br />
<p class="Error"> @ViewBag.ErrMessage </p>
<p>
<a href="Login"> <input type="submit" value="Create" /> </a>
</p>
</fieldset>
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(tblUser tbluser)
{
string val = System.Configuration.ConfigurationManager.AppSettings["CUTypeId"];
int code = Convert.ToInt32(val);
Session["Emailid"] = tbluser.EmailID.Trim();
if (ModelState.IsValid && this.IsCaptchaValid("Captcha Not Valid") )
{
EmailManager.SendConfirmationEmailName(tbluser.EmailID, tbluser.FirstName);
tbluser.UserTypeId = code;
db.tblUsers.Add(tbluser);
db.SaveChanges();
return RedirectToAction("Login", new { Email = tbluser.EmailID });
}
ViewBag.BusinessCategoryId = new SelectList(db.tblBusinessCategories, "BusinessID", "BusinessName", tbluser.BusinessCategoryId);
ViewBag.UserTypeId = new SelectList(db.tblUserTypes, "UserTypeID", "UserType", tbluser.UserTypeId);
ViewBag.AddressId = new SelectList(db.tblAddresses, "AddressID", "Address1", tbluser.AddressId);
ViewBag.ErrMessage = "Error: Captcha not valid";
return View(tbluser);
}