ASP. net http module and processing program module implementation (1)

Source: Internet
Author: User

Implement an HTTP module that provides security services

Now we implement an HTTP module that provides security services for our Web applications. The HTTP module provides a customized identity authentication service. It will receive the identity credential in the HTTP request and determine whether the credential is valid. If valid, what are the user-related roles? The User. Identity object is used to associate these roles with the User IDs that access our Web application page.

The code of the HTTP module is as follows:

 
 
  1. Using System;
  2. Using System. Web;
  3. Using System. Security. Principal;
  4.  
  5. Namespace SecurityModules
  6. {
  7. /// Overall description of Class1.
  8.  
  9. Public class CustomAuthenticationModule: IHttpModule
  10. {
  11. Public CustomAuthenticationModule ()
  12. {
  13. }
  14. Public void Init (HttpApplication r_objApplication)
  15. {
  16. // Register the event handler with the Application object.
  17. R_objApplication.AuthenticateRequest + =
  18. New EventHandler (this. AuthenticateRequest );
  19. }
  20.  
  21. Public void Dispose ()
  22. {
  23. // This field is empty because we do not need to perform any operations.
  24. }
  25.  
  26. Private void AuthenticateRequest (object r_objSender, EventArgs r_objEventArgs)
  27. {
  28. // Identify the user's creden。 and find out the user role ..
  29. 1. HttpApplicationObjApp= (HttpApplication) r_objSender;
  30. 2. HttpContextObjContext= (HttpContext) objApp. Context;
  31. 3. if (objApp. Request ["userid"] = null) |
  32. 4. (objApp. Request ["password"] = null ))
  33. 5 .{
  34. 6. objContext. Response. Write ("<H1> Credentials not provided </H1> ");
  35. 7. objContext. Response. End ();
  36. 8 .}
  37.  
  38. 9. stringUserid="";
  39. 10.Userid=ObjApp. Request ["userid"]. ToString ();
  40. 11. stringPassword="";
  41. 12.Password=ObjApp. Request ["password"]. ToString ();
  42. 13. string [] strRoles;
  43. 14.StrRoles=AuthenticateAndGetRoles(Userid, password );
  44. 15. if ((StrRoles= Null) | (strRoles. GetLength (0) = 0 ))
  45. 16 .{
  46. 17. objContext. Response. Write ("<H1> We are sorry but we cocould not
  47. Find this user id and password in our database </H1> ");
  48. 18. objApp. CompleteRequest ();
  49. 19 .}
  50.  
  51. 20. GenericIdentityObjIdentity=NewGenericIdentity (userid,
  52. "CustomAuthentication ");
  53. 21.ObjContext. User=NewGenericPrincipal (objIdentity, strRoles );
  54. }
  55.  
  56. Private string [] AuthenticateAndGetRoles (string r_strUserID, string r_strPassword)
  57. {
  58. String []StrRoles=Null;
  59. If (r_strUserID.Equals ("Steve") & (r_strPassword.Equals ("15 seconds ")))
  60. {
  61. StrRoles=NewString [1];
  62. StrRoles [0] = "Administrator ";
  63. }
  64. Else if (r_strUserID.Equals ("Mansoor") & (r_strPassword.Equals ("mas ")))
  65. {
  66. StrRoles=NewString [1];
  67. StrRoles [0] = "User ";
  68. }
  69. Return strRoles;
  70. }
  71. }
  72. }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.