Chapter 5: Design and implement security
Configure authentication
Authenticating users
IIS authentication
- Anonymous
- ASP.net impersonation
- Basic
transmit username/password between client/server in Base64 encoded but not encrypted. - Digest
username/password are encrypted - Forms
1: without using built-in windows security system
2: use FormsAuthentication.SetAuthCookie to make authentication token available for the rest of the session. - Windows
supported only in microsoft browser
use NTLM/Kerberos
straightforward and easy to implement, especially on intranet. - ASP.net impersonation authentication
independent of authentication mode configured in Web.config file
System.Security.Principal.IPrincipal / System.Security.Principal.IIdentity
- WindowsIdentity/WindowsPrincipal
- FormsIdentity/GenericPrincipal
- GenericIdentity/GenericPrinciapl
use AuthorizeAttribute to enforce authentication
- Form authentication + SimpleMembership + WebSecurity
Windows authentication- use Active directory to manage users
- all users are members of your domain
- require users to use IE or Microsoft browser
Form authentication - use standard ASP.net membership provider db schema or your own
Custom authentication - create a custom provider by implementing IIdentity or IPrincipal to interact with underlying authentication mechanism
Manage user session by cookies
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
DateTime.Now.AddDays(90),
createPersistentCookie, // a Boolean indicating whether a cookie
// should be created on the user's machine
String.Join(";",rolesArr) //user's roles
);
// add cookie to response stream
string encTicket = FormsAuthentication.Encrypt(authTicket);
System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.
FormsCookieName, encTicket);
System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
Configuring membership providers
- use SimpleMembershipProvider/WebSecurity helper classes
Creating custom membership providers
- ActiveDirectoryProvider for app use windows authentication
- SqlMembershipProvider for form authentication
Configure and apply authorization
- create roles
- configure roles
configure a SQL membership role provider in Web.config file
use InitializeDatabaseConnection(...) for SimpleMembershipProvider with SimpleRole. - Authorizing roles programmatically
- applying Authorize attribute
- check in code via followings
*RoleProvider.GetRolesForUser, RoleProvider.IsUserInRole, HttpContext.User.IsInRole
*WebSecurity.RequireRoles(...)
- creating custom role providers
- Implementing WCF service authorization
Design and implement claims-based authentication across federated identity stores
- Implementing federated authentication by using Windows Azure Access Control Service (ACS)
ACS features includes:- integrates with Windows Identity Foundation (WIF)
- support well-known identity providers such as Facebook, Microsoft account, Yahoo and Google
- support Active Directory Federation Services (ADFS) 2.0
- support OAuth 2.0, WS-Trust and WS-Federation protocols
- support various token formats, include JSON Web Token (JWT), Security Assertion Markup Language (SAML) and Simple Web Token (SWT)
- provides a web-based management portal

- Creating a custom security token by using WIF
- Handling token formats for SAML and SWT tokens
Manage data integrity
encryption terminology
- Encryption: DES, AES
- Hashing: MD5, SHA
- Salting
Applying encryption to application data
- Symmetric: AES, DES, RC2, Rijindael, TripleDES
- Asymmetric: DSA, ECDiffieHellman, ECDsa, RSA
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
{
// assumes that the key and initialization vectors are already configured
CryptoStream crypoStream = new CryptoStream(myManagedStream, rijndaelManaged.
CreateEncryptor(),CryptoStreamMode.Write);
};
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
{
// assumes that the key and initialization vectors are already configured
CryptoStream crypoStream = new CryptoStream(myManagedStream, rijndaelManaged.
CreateDecryptor(),CryptoStreamMode.Read);
};
Applying encryption to the configuraion sections of an application
- DPAPIProtectedConfigurationProvider
- RsaProtectedConfigurationProvider: allow export/import of the keys used for encryption/decryption
- use aspnet_regiis to encrypt/decrypt sections of the Web.confg file.
Signing application data to prevent tampering
// create the hash code of the text to sign
SHA1 sha = SHA1.Create();
byte[] hashcode = sha.ComputeHash(TextToConvert);
// use the CreateSignature method to sign the data
DSA dsa = DSA.Create();
byte[] signature = dsa.CreateSignature(hashcode);
// create the hash code of the text to verify
SHA1 sha = SHA1.Create();
byte[] hashcode = sha.ComputeHash(TextToVerify);
// use the VerifySignature method to verify the DSA signature
DSA dsa = DSA.Create();
bool isSignatureValid = dsa.VerifySignature(hashcode, signature);
Implement a secure site with ASP.NET
Securing communication by applying SSL/TLS certificates
setup site with certificate and httpsSalt and hash passwords for storage
Using HTML encoding to prevent cross-site scripting attacks (AntiXSS Library)
- use @Html.Encode()
- encode the data before saving to db
- use AntiXSS library from NuGet
Implementing deferred validation and handle unvalidated requests
Preventing SQL injection attacks by parameterizing queries
Preventing cross-site request forgeries (XSRFs)
[RequireSession]
[AcceptVerbs(HttpVerbs.Post)]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(string username, string password, string remember, string deviceToken, string apid)
{
}
@using (Html.BeginForm("Login", "Authorize"))
{
@Html.AntiForgeryToken();
}
internally cookie is used for XSRF validation.
Chapter 5: Design and implement security的更多相关文章
- Chapter 2: Design the user experience
Apply the user interface design for a web application 介绍了Css的常用属性和html5的新element,以及Htmlhelper的简单方法,如 ...
- Chapter 1: Design the application architecture
1.1 Plan the application layers 提到了repository pattern,SoC(Separation of Concern), 进而提及MVC,Action/Act ...
- Chapter 7. Design and Performance
本章将对MPEG4及H.264的实现细节进行讲解和比对. Motion Estimation 衡量运动估计的好坏有三种函数(第228页):MSE,MAE和SAE,其中由于SAE运算速度最快所以采用的最 ...
- MapReduce Design Patterns(chapter 1)(一)
Chapter 1.Design Patterns and MapReduce MapReduce 是一种运行于成百上千台机器上的处理数据的框架,目前被google,Hadoop等多家公司或社区广泛使 ...
- (转)MapReduce Design Patterns(chapter 1)(一)
翻译的是这本书: Chapter 1.Design Patterns and MapReduce MapReduce 是一种运行于成百上千台机器上的处理数据的框架,目前被google,Hadoop等多 ...
- Page Security
参见开发文档 Overview This document describes how to build applications that grant selected access to indi ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- Quality in the Test Automation Review Process and Design Review Template
About this document Prerequisite knowledge/experience: Software Testing, Test Automation Applicable ...
- Security Software Engineer
Security Software Engineer Are you excited to be part of the VR revolution and work on cutting edge ...
随机推荐
- dedecms qq咨询平均分配
qq后台页: qq_admin.php <style type="text/css"> <!-- * {margin:0; padding:0;} .wrap { ...
- Windows 10 RTM 官方正式版
Windows 10 各版本区别: Windows 10 家庭版:供家庭用户使用Windows 10 专业版:供小型企业使用 在家庭版基础上增加了域账号加入.bitlocker.企业商店等功能Wind ...
- SharePoint Framework 概述
博客地址:http://blog.csdn.net/FoxDave 本文翻译自新出的SharePoint Framework概述介绍文章,原文地址:http://dev.office.com/sh ...
- Android开源框架:Universal-Image-Loader解析(二)MemoryCache
- 修改 Docker 默认网桥地址
在公司里搭建docker测试环境,需要访问内部的服务, 由于网段是172.17.导致该容器没有办法正常访问公司内部服务.翻了一下官方的帮助文档,找到了修改默认网桥地址的办法. 首先停止正在使用的 Do ...
- CreateFile函数详解
CreateFile函数详解 CreateFile The CreateFile function creates or opens the following objects and returns ...
- PHPSTORM模板变量注释
类似于这种注释,方便使用. 有两种方式一种是生成PHP文件时,自动生成,一种是手动生成. 第一种:自动生成 一图解释所有.这么配置就OK了. 这种方法还有一种就是,在包括里边编写,直接引用,先写PHP ...
- python bytes to string
python bytes 转化成 string 会遇到如下错误: codec can't decode byte 0xff in position 5: illegal multibyte seque ...
- js 自运行函数作用
var obj = new Object(); function test2() { for (var i=1;i<5;i++) { obj['f'+i] = function() { retu ...
- Nginx配置指定媒体类型文件强制下载
由于业务需要,在点击显示链接(如www.xxx.com/2015-01-15/xxx.png)显示媒体资源(如图片.视频.音频.文档),而在点击下载链接(如www.xxx.com/2015-01-15 ...