首页 技术 正文
技术 2022年11月7日
0 收藏 763 点赞 732 浏览 3513 个字

一、Forms认证

1、在webapi项目中启用Forms认证

Why:为什么要在WebAPI中使用Forms认证?因为其它项目使用的是Forms认证。

What:什么是Forms认证?它在WebAPI中就是一个MessageHandle,具体请查找关键字“ASP.NET Forms

How:如何启动Forms认证?

  最简单的是通过配置启动Forms认证:

 <system.web>
<authentication mode="Forms">
<forms name=".FormsCookie" loginUrl="/login.aspx" protection="All" timeout="43200" path="/" defaultUrl="http://www.cnblogs.com" domain=".cnblogs.com" cookieless="UseCookies" />
</authentication>
<httpCookies httpOnlyCookies="true" />
</system.web>
<system.webServer>

  简单说说Forms认证的工作原理:首先在管道中,Forms读取请求中的相关的cookie,解密,进行认证,并把认证的结果写到请求上下文和线程的Identity属性中。然后请求继续往后面走,最终生成的响应在管道中返回时,Forms会判断如果响应为401,那么就location到配置中的loginUrl设置的地址,并改变status为302。

2、几个Attribute

Why:为什么要认识Attribute?因为Forms认证的结果是写到Identity属性中,一般我们要获取该属性,判断是否认证成功,如果失败返回401,等等进行很多处理。是不是很麻烦?对,封装起来,自己写一个吗?当然可以,其实微软大法早就考虑好了,针对一般的场景的处理逻辑都封装好了,他们分别叫做

AuthorizeAttribute、AllowAnonymousAttribute,都是Attribute。

What:这些Attribute是什么?顾名思义,AuthorizeAttribute只允许认证通过的请求,AllowAnonymousAttribute允许匿名请求。

How:那么该怎么用呢?很简单他们可以作用类型、方法上,所以可以全局注册、controller、action, so easy!

3、重写unauthorize中验证失败方法

Why:因为如果response status == 401,那么Forms会location到配置中的loginUrl,(即使没有手动配置它,也会生成一个默认值“login.aspx”),并且设置status为302。如果客户端是浏览器的话,那么就会直接进行跳转而无法捕获这个状态,这在很多场景下不合适,例如:spa(单页应用)中,我们不希望它自动跳转到登陆页面,而是给出提示,让用户自己选择是否登录。所以要重写Forms中身份验证失败的处理逻辑。

How:在AuthorizationFilterAttribute中有个虚方法HandleUnauthorizedRequest,重写它来实现自定义的处理逻辑。这样的设计思路挺不错,可以多借鉴。

    /// <summary>
/// If unauthorize return 403 instead of 401, avoid redirect.
/// </summary>
public class ForbiddenLocationAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = System.Net.HttpStatusCode.Forbidden;
actionContext.Response = response; }
}

使用403(Forbidden)来代替401,这样就可以避免Forms的自动跳转了。虽然这样做有些弊端,但这也不失为一个有效的解决办法。

二、ModelValidata(模型验证)

1、Why

  凡是有用户输入的地方都少不了参数验证,这不光是个安全问题,也是为了保证数据完整正确。

2、What

  WebAPI集成了模型验证机制,当请求被action执行之前,有一个模型绑定的步骤,它就是匹配action的参数,具体细节就不说了,ModelValidata就是在这里进行,它会根据Model中各个属性的DataAnnotations(数据注解)来进行验证,最终把结果保存在action的上下文的一个属性中,即actionContext.ModelState.IsValid。

3、How

  a、为Model设置DataAnnotations

  

public class BannerDto
{
[JsonProperty(PropertyName = "id")]
[Required(ErrorMessage = "Id是必须的")]
public Guid Id { get; set; } [JsonProperty(PropertyName = "title")]
[Required(ErrorMessage = "标题不能为空")]
[MaxLength(, ErrorMessage = "标题不能超过200个字符")]
public string Title { get; set; } [JsonProperty(PropertyName = "src")]
[Required(ErrorMessage = "图片链接不能为空")]
[MaxLength(, ErrorMessage = "图片链接不能超过500个字符")]
public string ImageUri { get; set; } [JsonProperty(PropertyName = "href")]
[Required(ErrorMessage = "超链接不能为空")]
[MaxLength(, ErrorMessage = "超链接不能超过500个字符")]
public string Href { get; set; } [JsonIgnore]
public Guid? AuthorityId { get; set; } [JsonIgnore]
public bool IsDeleted { get; set; } = false; [JsonProperty(PropertyName ="createDate")]
public DateTime CreateDate { get; set; }
}

  ps:[JsonProperty]、[JsonIgnore]是指定Json序列化的一些相关设置,设置别名、忽略等等。返回优雅的变量的名称,保证代码风格。

  DataAnnotations的使用,请查看msdn,太简单了。设置好约束条件和ErrorMessage,当验证失败了,就会返回ErrorMessage。

  b、使用Filter方式,为Action添加验证,好处就不多说了。

public class ValidataModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}

别忘了在HttpComfiguration中注入这个Filter

            //模型验证
config.Filters.Add(new ValidataModelAttribute());

对于不希望不验证的Action可以使用OverrideActionFilters重写上级设置的所有Fiters。

ModelValidate失败的请求会得到400的响应,同时所有ErrorMessage都会在响应报文中,例如:

{"Message":"The request is invalid.","ModelState":{"sub.Href":["超链接不可为空"]}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774