Asp.Net Web Api基于令牌的授权,没有OWIN和AspNet.Identity
我打算使用下面的代码来实现我的web api安全性,但我不确定这是否足够安全和??合理.我不想使用OWIN和AspNet.Identity,因为它对我来说非常复杂,我不完全理解,我不知道我如何定制数据库表,用户角色等.但我的方式很简单,非常可定制我.
这是CustomAuthorizeAttribute; public class CustomAuthorize : AuthorizeAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if ((actionContext.Request.Headers.GetValues("Host").FirstOrDefault().Contains("localhost:15742"))) { IEnumerable<string> access_token; if (actionContext.Request.Headers.TryGetValues("Authorization",out access_token)) { var user = GetUserByToken(access_token); if (user!=null && !user.TokenIsExpired) { HttpContext.Current.Response.AddHeader("WWW-Authenticate","Custom " + access_token.FirstOrDefault()); return; } else { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); HttpContext.Current.Response.AddHeader("WWW-Authenticate","Custom"); return; } } else { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); } } else { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden); } } } 这是前端使用 <script type="text/javascript"> $(function () { var access_token = $.cookie('access_token'); if (access_token == undefined) { $.cookie('access_token','test-token'); } $.ajax({ url: '/api/account',headers: { access_token: access_token },success: function (data) { document.write(data.name + " " + data.lastname); } }); }); </script> 顺便说一句,我为我的英语感到抱歉.我希望你理解我的问题,我在等你的建议. 解决方法
Necroreply为那些想要制作自定义身份验证属性的人:)
首先检查是多余的,因为HTTP请求只是TCP连接上的一串文本,因此任何人都可以使用TCP客户端连接到您的服务器并发送他想要的任何标头. 根据https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api
属性的唯一方法是不设置响应是用户!= null&& !user.TokenIsExpired所以这个属性可以完成工作,可以认为是安全的. 这个头可以删除HttpContext.Current.Response.AddHeader(“WWW-Authenticate”,“Custom”); 另外,为什么你会在成功的情况下再次发送身份验证令牌? HttpContext.Current.Response.AddHeader(“WWW-Authenticate”,“Custom”access_token.FirstOrDefault()); 只需减少IF-s嵌套级别,以便代码更易于阅读: public override void OnAuthorization(HttpActionContext actionContext) { IEnumerable<string> access_token; if (!actionContext.Request.Headers.TryGetValues("Authorization",out access_token)) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); return; } var user = GetUserByToken(access_token); if (user == null || user.TokenIsExpired) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); return; } // OK return; } 许多ASP.NET代码似乎过度设计(有时是:)包括OWIN.但它有一个目的 – 声明一种标准的方式来做各种各样的事情,例如auth. 想象一下,每个人都会开始构建他们的自定义attrbiutes,然后就不可能只安装Google nuget包并做类似的事情 public void ConfigureAuth(IAppBuilder app) { app.UseGoogleAuthentication( clientId: "000-000.apps.googleusercontent.com",clientSecret: "00000000000"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 深蓝词库转换1.9发布——支持英库拼音、搜狗bin格
- asp.net-mvc – Knockout,CKEditor&Single Page App
- 经典ASP页面的性能测试?
- 将配置文件appsetting中的值转换为动态对象调用
- .NET Core中Object Pool的简单使用
- .net core 中的经典设计模式的应用
- asp.net-mvc – 在Html.ActionLink的linkText中使用HTML标签
- asp.net-mvc-4 – 如何在ASP.NET MVC 4中使用免费的CSS模板
- 【DevExpress v17.2新功能预告】DevExpress ASP.NET Schedu
- ASP.NET网站项目何时重新编译?
- asp.net-core – 如何让ASP.Net 5应用程序能够使
- 日志记录 – ASP.NET MVC 4 Web Api:诊断功能
- asp.net – BC30560:’ExtensionAttribute’在名
- asp.net-mvc – 如何使用ASP.NET MVC创建仪表板用
- .net – 将HtmlTextWriter的内容转换为字符串
- asp.net-mvc – 更改表中给定行的背景颜色
- asp.net – Response.Redirect()不起作用
- 缓存ASP.NET HTTP Handler服务器和客户端的响应
- asp.net – 在Visual Studio中的单独设计和源选项
- 我想学习ASP.Net 2.0 / 3.5 / 4.0我应该从ASP.Ne