c# - Basic authentication on a remote server -


i need asmx web-services.

let's suppose have serverservice provides data. let's suppose has method getrandominteger returns random integer (obviously). implements custom basic authentication using ihttpmodule.

public class basicauthhttpmodule : ihttpmodule {     private userrepository _userrepository;      public void dispose()     {     }      public void init(httpapplication application)     {         _userrepository = new userrepository();         application.authenticaterequest += onauthenticaterequest;         application.endrequest += onendrequest;     }      public void onauthenticaterequest(object source, eventargs e)     {         var app = (httpapplication)source;          string authheader = app.request.headers["authorization"];         if (!string.isnullorempty(authheader))         {             // here credentials header              if (_userrepository.validateuser(username, password)) return;              // return 401 , completerequest         }         else         {             // return 401 , end         }     }      public void onendrequest(object source, eventargs eventargs)     {         if (httpcontext.current.response.statuscode == 401)         {                // return 401 , require new authorization         }     } 

fortunately, works. can open service.asmx file, basic authentication window , access it's getrandominteger method after successful authentication.

now have asp.net mvc 4 application called clientservice. must provide user interface convenient , appropriate access methods of serverservice. has default controllers account , home, default views etc.

  1. i need clientservice authenticate on serverservice. mean there home/index page button "login". enter login , password there , clientservice tries authenticate @ serverservice. returns error on fail or authenticates on success providing access home/randomint page show integer requested serverservice. best , easiest way this?

  2. how implement registration on serverservice? there no allowanonymous attribute or @ asmx, can't register user because doesn't have access of methods due 401 error.

thank in advance.

p.s. no. can't use wcf or else. need implement asmx web-service.

update 1: ok, have learned new here

http://www.aspsnippets.com/articles/how-to-add-reference-of-web-service-asmx-in-aspnet-using-visual-studio.aspx

there old-style thing "web reference" , it's not "service reference". have added web reference project , can call methods asmx page in way:

        try         {             serverservice svc = new serverservice();             svc.credentials = new networkcredential("user", "password");             int = svc.getrandominteger();         } catch (webexception e) {             // auth failed         } 

however, don't understand how link asp.net mvc clientservice authentication. so, both questions still open. hopefully, understand or me.

here documentation adding web reference asmx service.

http://www.aspsnippets.com/articles/how-to-add-reference-of-web-service-asmx-in-aspnet-using-visual-studio.aspx

using information can make requests web service.

the thing left on moment of question update create custom authentication.

  1. when user logins, client sends request service. in case of successful basic authentication, creates proper formsauthentication cookie ticket user. user logs in.

  2. on each request service, client extracts login formsauthentication cookie , password server cache , uses them authenticate on service. in case of basic auth failure (it can occur if user's password has been changed on service side) cookie cleared , session aborted.

  3. registration implemented using 1 asmx service not using basic auth anonymous (because registration supposed anonymous method).

that's it. finally, have found proper solution :)


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -