Use httpmodel to integrate with existing websites based on shared login information (cookies)

Source: Internet
Author: User
/*************************************** * ************************************ Function Description: * 1. Use httpmodel to integrate with existing websites based on shared login information (cookies) * 2. Automatic user logon, automatic registration, and delayed registration, * 3. synchronous exit ** usage: * See the sample Web. config ** design code: *************************************** * ***********************************/using system; using system. collections. generic; using system. text; using system. web; using system. web. secu Rity; using wojilu. open; namespace wojilu. authenticationmodule {public class authenticationmodule: system. web. ihttpmodule {public void dispose () {// throw new notimplementedexception ();} public void Init (httpapplication context) {context. beginrequest + = new eventhandler (application_beginrequest); context. endrequest + = new eventhandler (application_endrequest); context. authenticaterequest + = new Eventhandler (context_authenticaterequest);} void context_authenticaterequest (Object sender, eventargs e) {httpapplication application = sender as httpapplication; httpcontext context = application. context; httprequest Req = context. request; // define httpresponse response = context for testing. response; openservice = new openservice (); If (req. isauthenticated) {If (openservice. userislogin (Conte XT )! = True) {string username = context. user. identity. name; // response. write ("username =" + username); If (openservice. userisregister (username, context) {openservice. userlogin (username, context); this. updateuserprofile (username);} else {string userpwd = getrandomnumberstring (16); string useremail = string. format ("{0} @ lcsyzx.cn", username); openservice. userregister (username, userpwd, useremail, null, "Home, blog, photo, microblog, friend, visitor, forumpost, about, feedback, share") ;}} else {// response. write ("isauthenticated = false"); openservice. userlogout (context) ;}} public void application_beginrequest (Object sender, eventargs e) {} public void application_endrequest (Object sender, eventargs e) {// httpapplication application = sender as httpapplication; // httpcontext context = applica Tion. context; // httpresponse response = context. response; // response. write ("This Is An endrequest from the custom httpmodule ");} /// <summary> /// update the real name of the profile to wojilu // </Summary> /// <Param name = "USR"> </param> private void updateuserprofile (string username) {USERPROFILE = new USERPROFILE (); // (USERPROFILE) profilebase. create (USR. username, true); USERPROFILE. initialize (username, true); wojilu. memb ERS. users. domain. user USR = new openservice (). getuserbyname (username); // username loginusername fullname usergroup curji curbh curbnbh if (USERPROFILE. usergroup = "student") USR. realname = string. format ("{0} ({1}) class {2}", USERPROFILE. curji, USERPROFILE. curbh, USERPROFILE. fullname); else if (USERPROFILE. usergroup = "instructor") USR. realname = USERPROFILE. fullname; else USR. realname = string. format ("{0} {1}", use Rprofile. usergroup, USERPROFILE. fullname); USR. Pwd = getrandomnumberstring (16); If (USR. Name! = "Admin") USR. update (); // dB. update (USR, "realname"); // dB. update (USR, "PWD ");} /// <summary> /// generate a random number string /// </Summary> /// <Param name = "int_numberlength"> Number length </param> /// <returns> </returns> private string getrandomnumberstring (INT int_numberlength) {bool onlynumber = false; random = new random (); string strings = "123456789"; if (! Onlynumber) strings + = "abcdefghjkmnpqrstuvwxyz"; char [] chars = strings. tochararray (); string returncode = string. empty; For (INT I = 0; I <int_numberlength; I ++) returncode + = chars [random. next (0, chars. length)]. tostring (); Return returncode ;}}}

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.