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 ...
随机推荐
- Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...
- mybatis学习2
解决字段名与实体类属性名不相同的冲突 1. 准备表和数据:CREATE TABLE orders(order_id INT PRIMARY KEY AUTO_INCREMENT,order_no VA ...
- WPF项目中所遇到的一些问题集
1. 没有Timer控件 解决方案: 第一步:申明一个DispatcherTimer 类的变量, private DispatcherTimer timer; //定时控件 第二步:初始化这个类 ti ...
- SQLSERVER2008 不能用IP连接数据库问题
本机安装的数据库只能用电脑名+"\"+实例名来访问,用“.”.“(local)”.“localhost”呀都不能访问.然后在网上挖资料呀,挖呀挖呀,终于挖到了解决办法. 1.不能用 ...
- 吐槽THINKPHP5命令行
thinkphp,作为国内开源框架,一直在使用和学习. 但是实在忍不住想要吐槽一下他的开发文档,和 对初学者的不友好,建议刚接触MVC思想的人 还是尽量去使用其他框架当入门. 现在来吐槽一下think ...
- Maven Test
Failures表示要测试的结果与预期值不一致:Errors表示测试代码或产品代码发生了未预期的错误:Skipped表示那些被标记为忽略的测试方法.在Junit中用户可以使用@Ignore注解标记忽略 ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- HTML5新增标签及具体用法
html5自从推广普及以来,迅速被各大浏览器支持.采用html5设计的网页逐渐成为网页设计的时尚.下面就温习下html5的新增标签. HTML 5 中的新特性包括了嵌入音频.视频和图形的功能,客户端数 ...
- 世界超强完美DIY 电子奇才五年全手工制作CPU
世界超强完美DIY 电子奇才五年全手工制作CPU 2015-07-08 极客范 (点击上方公众号,可快速关注我们) 在如今越来越靠程序化.流水线作业来完成生产的制造业中,想找一件手工打造的产品,真是越 ...
- ubuntu samba 服务器设置
安装 SAMBA 组件 sudo apt-get install samba smbfs smbclient ubuntu 14.04 使用以下方式安装: ? 1 2 3 4 5 6 7 若之前有安装 ...