后台:
using System;
using System.Web;
using System.Web.Mvc;
namespace EwayFramework.Utils.Token
{
public static class HtmlTokenHelper
{
public static MvcHtmlString GenerateVerficationToken(this HtmlHelper htmlhelper)
{
string formValue = SessionPageTokenView.Encrypt(HttpContext.Current.Session.SessionID + DateTime.Now.Ticks.ToString());
string sessionname = HttpContext.Current.Request.Path + HttpContext.Current.Request.Params["ID"];
HttpContext.Current.Session[sessionname] = formValue;
string fieldName = PageTokenViewBase.HiddenTokenName;
TagBuilder builder = new TagBuilder("input");
builder.Attributes["type"] = "hidden";
builder.Attributes["name"] = fieldName;
builder.Attributes["value"] = formValue;
return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));
}
}
}
前台调用:
@using EwayFramework.Utils.Token
@html.GenerateVerficationToken()
结果:
会自动生成隐藏控件