Hello. I am calling my Web Api using jquery ajax function and passing username and password parameters, but how do I read those on the other side, in c#?
Depends how the service authenticates. A common approach would be to use HTTP Basic authentication. The
Thinktecture.IdentityModel
security library has support for basic auth in WebAPI. Here's a blog post on the usage in the server:
Why a custom library? Isn't there something built in?
Nothing built-in, really, unless you could IIS' security model (and thus authenticating against windows accounts) -- you could have it do basic auth and then you'd not need the custom library.
If you use Forms authentication then there's really nothing special your Ajax codes needs to do, as the browser will automatically pass the forms auth cookies on the Ajax calls and Forms auth will automaitcally read the cookie to authenticate the call. So
in a sense if the web pages are already authenticating with Forms then this is a very easy way to achieve authentication in your WebAPI code.
dmitri_vagan...
Member
6 Points
13 Posts
How to check username and password in Web Api
Aug 02, 2012 06:37 PM|LINK
Hello. I am calling my Web Api using jquery ajax function and passing username and password parameters, but how do I read those on the other side, in c#?
function submitRequest() { $.ajax({ url: "api/PharmacyNotes", contentType: "text/xml", type: "POST", cache: false, data: "<GetPharmacyNotesRequest><RequestID>" + $("#requestId").val() + "</RequestID><DocumentID>" + $("#groupNumber").val() + "</DocumentID></GetPharmacyNotesRequest>", dataType: "xml", username: "user", password: "pass", success: getResponseUnparsedXml }); }BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to check username and password in Web Api
Aug 02, 2012 07:50 PM|LINK
Depends how the service authenticates. A common approach would be to use HTTP Basic authentication. The Thinktecture.IdentityModel security library has support for basic auth in WebAPI. Here's a blog post on the usage in the server:
http://leastprivilege.com/2012/05/26/thinktecture-identitymodel-and-asp-net-web-api/
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dmitri_vagan...
Member
6 Points
13 Posts
Re: How to check username and password in Web Api
Aug 02, 2012 08:48 PM|LINK
Why a custom library? Isn't there something built in?
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to check username and password in Web Api
Aug 02, 2012 08:55 PM|LINK
Nothing built-in, really, unless you could IIS' security model (and thus authenticating against windows accounts) -- you could have it do basic auth and then you'd not need the custom library.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
pacoalphonso
Member
138 Points
63 Posts
Re: How to check username and password in Web Api
Aug 03, 2012 05:49 AM|LINK
Hi,
Are you going to use Forms Authentication?
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: How to check username and password in Web Api
Aug 03, 2012 12:57 PM|LINK
If you use Forms authentication then there's really nothing special your Ajax codes needs to do, as the browser will automatically pass the forms auth cookies on the Ajax calls and Forms auth will automaitcally read the cookie to authenticate the call. So in a sense if the web pages are already authenticating with Forms then this is a very easy way to achieve authentication in your WebAPI code.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
dmitri_vagan...
Member
6 Points
13 Posts
Re: How to check username and password in Web Api
Aug 06, 2012 12:52 PM|LINK
The scenario I have is a client passes me XML via java call. Here is a sample:
I have to read out auth-username and auth-password and authanticate against our database.