AntiForgery令牌使用ASP.NET5 Web API而不使用NET46上的System.W
发布时间:2020-12-16 06:39:02 所属栏目:asp.Net 来源:网络整理
导读:尝试在ASP.NET5(aka vNext)API上实现AntiForgery 我找到的所有文章都来自this article并使用System.Web.Helpers.AntiForgery.GetTokens,这不应该是ASP.NET5的方式 private static string GetTokenHeaderValue() { string cookieToken,formToken; System.Web.
尝试在ASP.NET5(aka vNext)API上实现AntiForgery
我找到的所有文章都来自this article并使用System.Web.Helpers.AntiForgery.GetTokens,这不应该是ASP.NET5的方式 private static string GetTokenHeaderValue() { string cookieToken,formToken; System.Web.Helpers.AntiForgery.GetTokens(null,out cookieToken,out formToken); return cookieToken + ":" + formToken; } 是否有任何实现实际显示如何在ASP.NET5中检索这些令牌 其他来源ASP.NET5 AntiForgery Source Code 解决方法
在Controller处生成
using Microsoft.AspNet.Mvc; using Microsoft.Framework.DependencyInjection; namespace MyApp.App.Controllers { public class MyController : Controller { public string GetAntiForgeryTokens() { var antiForgery = Context.RequestServices.GetService<AntiForgery>(); AntiForgeryTokenSet antiForgeryTokenSet = antiForgery.GetTokens(Context,null); string output = antiForgeryTokenSet.CookieToken + ":" + antiForgeryTokenSet.FormToken; return output; } } } 在View中生成 @inject AntiForgery antiForgery @functions { public string GetAntiForgeryTokens() { AntiForgeryTokenSet antiForgeryTokenSet = antiForgery.GetTokens(Context,null); string output = antiForgeryTokenSet.CookieToken + ":" + antiForgeryTokenSet.FormToken; return output; } } <body> @GetAntiXsrfToken() </body> 验证 var antiForgery = Context.RequestServices.GetService<AntiForgery>(); antiForgery.Validate(Context,new AntiForgeryTokenSet(formToken,cookieToken)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 为什么不调用我的SelectCountMethod?
- asp.net – 承载令牌在重新部署后变为无效
- asp.net-core – 无法解析目标框架’DNX,Version = v4.5.1’
- asp.net – 什么时候回输HTML输入标签?
- asp.net – 在C#中解码Base64字节数组到图像
- 登录到MVC 5 ASP.NET模板从根文件夹移动时Web应用程序失败
- asp.net-mvc – 如何测试我的Web应用程序是否可以处理大量流
- asp.net – 微软azure smtp电子邮件中继设置
- 如何在没有布局页面的情况下将部分脚本添加到asp.net mvc R
- asp.net-mvc – DotNetOpenAuth在ASP.NET MVC中的OAuth
推荐文章
站长推荐
热点阅读