SlideShare une entreprise Scribd logo
1  sur  18
• Authentication and Authorization
• Forms Authentication
   • Web.config Settings
   • Authorization Rules
   • Controlling access to specific directories
   • Controlling access to specific files
   • Controlling access to specific users
   • Persistent Cookies
• Windows Authentication
   • Web.config Settings
   • A windows authentication test
Authentication: This is the process of determining a
user’s identity and forcing users to prove they are who
they claim to be. Usually, this involves entering
credentials (typically a user name and password) into
some sort of login page or window. These credentials
are then authenticated against the Windows user
accounts on a computer, a list of users in a file, or a
back-end database.

Authorization: Once a user is authenticated,
authorization is the process of determining whether
that user has sufficient permissions to perform a given
action (such as viewing a page or retrieving
information from a database).
Forms authentication: ASP.NET is in charge of
authenticating users, tracking them, and authorizing
every. Forms authentication is the best and most flexible
way to run a subscription site or e-commerce store.

Windows authentication: With Windows authentication,
the web server forces every user to log in as a Windows
user. This system requires that all users have Windows
user accounts on the server. This scenario is poorly
suited for a public web application but is often ideal with
an intranet or company-specific site designed to provide
resources for a limited set of users.
To implement forms-based security, you need to follow
three steps:

1. Set the authentication mode to forms authentication
in the web.config file. (If you prefer a graphical tool,
you can use the WAT during development or IIS
Manager after deployment.)

2. Restrict anonymous users from a specific page or
directory in your application.

3. Create the login page.
<configuration>
<system.web>
<authentication mode="Forms">
<forms name="MyAppCookie"
loginUrl="~/Login.aspx"
protection="All"
timeout="30" path="/" />
</authentication>
...
</system.web>
</configuration>
<configuration>
<system.web>
….
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" />
</authentication>

<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
...
</system.web>
</configuration>
Leave the default <authorization> settings in the
normal parent directory, and add a web.config file that
specifies stricter settings in the secured directory.
This web.config simply needs to deny anonymous users
(all other settings and configuration sections can be
omitted).
<!-- This web.config file is in a subfolder. -->
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" />
</authentication>
<authorization>
<allow users="*" />
</authorization>
</system.web>

<location path="AnotherSecuredPage.aspx">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
</configuration>
The <allow> and <deny> rules don’t need to use the
asterisk or question mark wildcards. Instead, they can
specifically identify a user name or a list of comma-
separated user names.
<authorization>
<deny users="?" />
<deny users="matthew,sarah" />
<deny users="john" />
<allow users="*" />
</authorization>
ASP.NET provides a special FormsAuthentication class
in the System.Web.Security namespace, which provides
static methods that help manage the process

public partial class Login : System.Web.UI.Page
{
protected void cmdLogin_Click(Object sender, EventArgs e)
{
if (txtPassword.Text.ToLower() == "secret")
{
FormsAuthentication.RedirectFromLoginPage(txtName.Text, false);
}
else
{
lblStatus.Text = "Try again.";
}
}
}
Once the user is logged in, you can retrieve the identity
through the built-in User property, as shown here:
protected void Page_Load(Object sender, EventArgs e)
{
lblMessage.Text = "You have reached the secured page, ";
lblMessage.Text += User.Identity.Name + ".";
}
You can access the User object in your code because it’s a
property of the current Page object. It has one property and
one method :
1. The Identity property lets you retrieve the name of the
    logged-in user and the type of authentication that was
    used.
2. • The IsInRole() method lets you determine whether a
    user is a member of a given role
A persistent authentication cookie remains on the
user’s hard drive and keeps the user signed in for
hours, days, or weeks—even if the user closes and
reopens the browser.

If you want to allow the user to create a persistent
cookie, you should make it optional, because the user
may want to access your site from a public or shared
computer. Generally, sites that use this technique
include a check box with text such as Keep Me
Logged In.
With Windows authentication, the web server takes care of the
authentication process. When you use Windows authentication,
you force users to log into IIS before they’re allowed to access
secure content in your website.

The user login information can be transmitted in several ways
but the end result is that the user is authenticated using a local
Windows account.

To implement Windows-based security with known users, you
need to follow three steps:

1. Set the authentication mode to Windows authentication in the
web.config file.
2. Disable anonymous access for a directory by using an
authorization rule.
3. Configure the Windows user accounts on your web server (if
they aren’t already present).
<configuration>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?" />
<allow roles=".SalesAdministrator,.SalesStaff" />
<deny users=".matthew" />
</authorization>
...
</system.web>
</configuration>
protected void Page_Load(Object sender, EventArgs e)
{
if (User.IsInRole(@"MyDomainNameSalesAdministrators"))
{
}
else
{
Response.Redirect("Default.aspx");
}
if (User.IsInRole(@"BUILTINAdministrators"))
{
// (Code goes here.)
}

}
Auth & Auth: Forms, Windows, Roles

Contenu connexe

En vedette (20)

Bread board
Bread boardBread board
Bread board
 
Breadboard
BreadboardBreadboard
Breadboard
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Intro To ECAT
Intro To ECATIntro To ECAT
Intro To ECAT
 
Chapter 25
Chapter 25Chapter 25
Chapter 25
 
CSS3 notes
CSS3 notesCSS3 notes
CSS3 notes
 
CSS
CSSCSS
CSS
 
HTML5 &CSS: Chapter 08
HTML5 &CSS: Chapter 08HTML5 &CSS: Chapter 08
HTML5 &CSS: Chapter 08
 
HTML & CSS: Chapter 07
HTML & CSS: Chapter 07HTML & CSS: Chapter 07
HTML & CSS: Chapter 07
 
Html and CSS: Chapter 02
Html and CSS: Chapter 02Html and CSS: Chapter 02
Html and CSS: Chapter 02
 
HTML & CSS: Chapter 03
HTML & CSS: Chapter 03HTML & CSS: Chapter 03
HTML & CSS: Chapter 03
 
HTML: Chapter 01
HTML: Chapter 01HTML: Chapter 01
HTML: Chapter 01
 
HTML & CSS: Chapter 06
HTML & CSS: Chapter 06HTML & CSS: Chapter 06
HTML & CSS: Chapter 06
 
CSS - Basics
CSS - BasicsCSS - Basics
CSS - Basics
 
HTML & CSS: Chapter 04
HTML & CSS: Chapter 04HTML & CSS: Chapter 04
HTML & CSS: Chapter 04
 
Unit 6, Lesson 3 - Vectors
Unit 6, Lesson 3 - VectorsUnit 6, Lesson 3 - Vectors
Unit 6, Lesson 3 - Vectors
 
Basic css
Basic cssBasic css
Basic css
 
Web Engineering - Basic CSS Properties
Web Engineering - Basic CSS PropertiesWeb Engineering - Basic CSS Properties
Web Engineering - Basic CSS Properties
 
Vernier caliper
Vernier caliperVernier caliper
Vernier caliper
 
Spline Interpolation
Spline InterpolationSpline Interpolation
Spline Interpolation
 

Similaire à Auth & Auth: Forms, Windows, Roles

Security asp.net application
Security asp.net applicationSecurity asp.net application
Security asp.net applicationZAIYAUL HAQUE
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11Vivek chan
 
Forms authentication
Forms authenticationForms authentication
Forms authenticationSNJ Chaudhary
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11Mani Chaubey
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11Niit Care
 
Authorization in asp
Authorization in aspAuthorization in asp
Authorization in aspOPENLANE
 
Introduction Yii Framework
Introduction Yii FrameworkIntroduction Yii Framework
Introduction Yii FrameworkTuan Nguyen
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3Neeraj Mathur
 
James Allardice - "Building a better login with the credential management API"
James Allardice - "Building a better login with the credential management API"James Allardice - "Building a better login with the credential management API"
James Allardice - "Building a better login with the credential management API"IT Event
 
Java Web Programming [9/9] : Web Application Security
Java Web Programming [9/9] : Web Application SecurityJava Web Programming [9/9] : Web Application Security
Java Web Programming [9/9] : Web Application SecurityIMC Institute
 
Windows Small Business Server 2011 Nasıl Kullanılır
Windows Small Business Server 2011 Nasıl KullanılırWindows Small Business Server 2011 Nasıl Kullanılır
Windows Small Business Server 2011 Nasıl KullanılırMustafa
 
Authentication
AuthenticationAuthentication
Authenticationsoon
 
Introduccion a la seguridad Windows 7
Introduccion a la seguridad Windows 7Introduccion a la seguridad Windows 7
Introduccion a la seguridad Windows 7EAE
 
Spring4 security
Spring4 securitySpring4 security
Spring4 securitySang Shin
 
Introduction to Azure AD and Azure AD B2C
Introduction to Azure AD and Azure AD B2CIntroduction to Azure AD and Azure AD B2C
Introduction to Azure AD and Azure AD B2CJoonas Westlin
 

Similaire à Auth & Auth: Forms, Windows, Roles (20)

Security asp.net application
Security asp.net applicationSecurity asp.net application
Security asp.net application
 
ASP.NET Lecture 5
ASP.NET Lecture 5ASP.NET Lecture 5
ASP.NET Lecture 5
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11
 
Forms authentication
Forms authenticationForms authentication
Forms authentication
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11
 
08 asp.net session11
08 asp.net session1108 asp.net session11
08 asp.net session11
 
Authorization in asp
Authorization in aspAuthorization in asp
Authorization in asp
 
Introduction Yii Framework
Introduction Yii FrameworkIntroduction Yii Framework
Introduction Yii Framework
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3
 
SCWCD : Secure web
SCWCD : Secure webSCWCD : Secure web
SCWCD : Secure web
 
SCWCD : Secure web : CHAP : 7
SCWCD : Secure web : CHAP : 7SCWCD : Secure web : CHAP : 7
SCWCD : Secure web : CHAP : 7
 
James Allardice - "Building a better login with the credential management API"
James Allardice - "Building a better login with the credential management API"James Allardice - "Building a better login with the credential management API"
James Allardice - "Building a better login with the credential management API"
 
Ekran system functions v. 5.0
Ekran system functions v. 5.0Ekran system functions v. 5.0
Ekran system functions v. 5.0
 
Java Web Programming [9/9] : Web Application Security
Java Web Programming [9/9] : Web Application SecurityJava Web Programming [9/9] : Web Application Security
Java Web Programming [9/9] : Web Application Security
 
Windows Small Business Server 2011 Nasıl Kullanılır
Windows Small Business Server 2011 Nasıl KullanılırWindows Small Business Server 2011 Nasıl Kullanılır
Windows Small Business Server 2011 Nasıl Kullanılır
 
SBS 2011 Kullanimi
SBS 2011 KullanimiSBS 2011 Kullanimi
SBS 2011 Kullanimi
 
Authentication
AuthenticationAuthentication
Authentication
 
Introduccion a la seguridad Windows 7
Introduccion a la seguridad Windows 7Introduccion a la seguridad Windows 7
Introduccion a la seguridad Windows 7
 
Spring4 security
Spring4 securitySpring4 security
Spring4 security
 
Introduction to Azure AD and Azure AD B2C
Introduction to Azure AD and Azure AD B2CIntroduction to Azure AD and Azure AD B2C
Introduction to Azure AD and Azure AD B2C
 

Plus de application developer (20)

Chapter 26
Chapter 26Chapter 26
Chapter 26
 
Chapter 23
Chapter 23Chapter 23
Chapter 23
 
Assignment
AssignmentAssignment
Assignment
 
Next step job board (Assignment)
Next step job board (Assignment)Next step job board (Assignment)
Next step job board (Assignment)
 
Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Week 3 assignment
Week 3 assignmentWeek 3 assignment
Week 3 assignment
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
C # test paper
C # test paperC # test paper
C # test paper
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
 
Chapter 8 part2
Chapter 8   part2Chapter 8   part2
Chapter 8 part2
 
Chapter 8 part1
Chapter 8   part1Chapter 8   part1
Chapter 8 part1
 
Chapter 7
Chapter 7Chapter 7
Chapter 7
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 

Dernier

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Dernier (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Auth & Auth: Forms, Windows, Roles

  • 1. • Authentication and Authorization • Forms Authentication • Web.config Settings • Authorization Rules • Controlling access to specific directories • Controlling access to specific files • Controlling access to specific users • Persistent Cookies • Windows Authentication • Web.config Settings • A windows authentication test
  • 2. Authentication: This is the process of determining a user’s identity and forcing users to prove they are who they claim to be. Usually, this involves entering credentials (typically a user name and password) into some sort of login page or window. These credentials are then authenticated against the Windows user accounts on a computer, a list of users in a file, or a back-end database. Authorization: Once a user is authenticated, authorization is the process of determining whether that user has sufficient permissions to perform a given action (such as viewing a page or retrieving information from a database).
  • 3. Forms authentication: ASP.NET is in charge of authenticating users, tracking them, and authorizing every. Forms authentication is the best and most flexible way to run a subscription site or e-commerce store. Windows authentication: With Windows authentication, the web server forces every user to log in as a Windows user. This system requires that all users have Windows user accounts on the server. This scenario is poorly suited for a public web application but is often ideal with an intranet or company-specific site designed to provide resources for a limited set of users.
  • 4. To implement forms-based security, you need to follow three steps: 1. Set the authentication mode to forms authentication in the web.config file. (If you prefer a graphical tool, you can use the WAT during development or IIS Manager after deployment.) 2. Restrict anonymous users from a specific page or directory in your application. 3. Create the login page.
  • 6.
  • 7. <configuration> <system.web> …. <authentication mode="Forms"> <forms loginUrl="~/Login.aspx" /> </authentication> <authorization> <deny users="?" /> <allow users="*" /> </authorization> ... </system.web> </configuration>
  • 8. Leave the default <authorization> settings in the normal parent directory, and add a web.config file that specifies stricter settings in the secured directory. This web.config simply needs to deny anonymous users (all other settings and configuration sections can be omitted). <!-- This web.config file is in a subfolder. --> <configuration> <system.web> <authorization> <deny users="?" /> </authorization> </system.web> </configuration>
  • 9. <configuration> <system.web> <authentication mode="Forms"> <forms loginUrl="~/Login.aspx" /> </authentication> <authorization> <allow users="*" /> </authorization> </system.web> <location path="AnotherSecuredPage.aspx"> <system.web> <authorization> <deny users="?" /> </authorization> </system.web> </location> </configuration>
  • 10. The <allow> and <deny> rules don’t need to use the asterisk or question mark wildcards. Instead, they can specifically identify a user name or a list of comma- separated user names. <authorization> <deny users="?" /> <deny users="matthew,sarah" /> <deny users="john" /> <allow users="*" /> </authorization>
  • 11. ASP.NET provides a special FormsAuthentication class in the System.Web.Security namespace, which provides static methods that help manage the process public partial class Login : System.Web.UI.Page { protected void cmdLogin_Click(Object sender, EventArgs e) { if (txtPassword.Text.ToLower() == "secret") { FormsAuthentication.RedirectFromLoginPage(txtName.Text, false); } else { lblStatus.Text = "Try again."; } } }
  • 12.
  • 13. Once the user is logged in, you can retrieve the identity through the built-in User property, as shown here: protected void Page_Load(Object sender, EventArgs e) { lblMessage.Text = "You have reached the secured page, "; lblMessage.Text += User.Identity.Name + "."; } You can access the User object in your code because it’s a property of the current Page object. It has one property and one method : 1. The Identity property lets you retrieve the name of the logged-in user and the type of authentication that was used. 2. • The IsInRole() method lets you determine whether a user is a member of a given role
  • 14. A persistent authentication cookie remains on the user’s hard drive and keeps the user signed in for hours, days, or weeks—even if the user closes and reopens the browser. If you want to allow the user to create a persistent cookie, you should make it optional, because the user may want to access your site from a public or shared computer. Generally, sites that use this technique include a check box with text such as Keep Me Logged In.
  • 15. With Windows authentication, the web server takes care of the authentication process. When you use Windows authentication, you force users to log into IIS before they’re allowed to access secure content in your website. The user login information can be transmitted in several ways but the end result is that the user is authenticated using a local Windows account. To implement Windows-based security with known users, you need to follow three steps: 1. Set the authentication mode to Windows authentication in the web.config file. 2. Disable anonymous access for a directory by using an authorization rule. 3. Configure the Windows user accounts on your web server (if they aren’t already present).
  • 16. <configuration> <system.web> <authentication mode="Windows" /> <authorization> <deny users="?" /> <allow roles=".SalesAdministrator,.SalesStaff" /> <deny users=".matthew" /> </authorization> ... </system.web> </configuration>
  • 17. protected void Page_Load(Object sender, EventArgs e) { if (User.IsInRole(@"MyDomainNameSalesAdministrators")) { } else { Response.Redirect("Default.aspx"); } if (User.IsInRole(@"BUILTINAdministrators")) { // (Code goes here.) } }