How to add username and password in Authorization header?
Currently I have the webAPI working like this.
I am getting the user information successfully. Now the requirement is that I should be able to log in with username and password. The way I am thinking to implement it is like this. Add the user-passed username and password in the request header with the token. Now there should be a check like is there a token? then check the token from the db. If yes, then validate the password etc.
What is the way to do it? public ActionResult Index().
How should I do to add the username and password in the request header (in javascript) with the token in the payload or whatever. If there is no token (for testing), how should I send the request to the DB where it should be validated.
You're already using the proper method by using the HttpContext. I would just create an action method on my controller that takes username and password as parameters and saves it into the database (I guess this might be your token). Public class UserController : ApiController. SQLconnection cn1 = new SQLconnection(@"Data Source=myserver.com;Initial Catalog=TESTDB;User Id=sa;Password=master"); cn1.Parameters.AddWithValue("@username", username);
cmd.
How do you pass a header in Python requests?
I've seen many threads on SO about this, but they all point to either of the following: sending a header with the call (or post). Adding the headers in the HTTP request before making the call. Neither of those seem to be working for me. Here's the code from my API: def rq(): rq = requests.Request('POST', u'login/session') rq.headers = rq.key).replace(";", "")
rq.url = self.site
return rq. I tested the API call from POSTMAN and it works. So I think my headers are good. Here's what happens when I call the API though.
HTTP ERROR 400: Bad Request. Headers: Can anyone see anything wrong here? EDIT: If I add the headers to the request (before passing the request to POSTMAN) it works fine, so I guess that's a clue. Also, the header is not a cookie, though it seems to be interpreted as such. The problem was I did not encode the headers. When I tried encoding them, and the cookies too, it worked!
How do you pass username and password in Python requests?
In the documentation for the requests library there is a section on how to use Basic Auth.
import requests. From requests.auth import HTTPBasicAuth r = requests.get(' auth=HTTPBasicAuth('user', 'password')) Requests uses urllib3 under the hood so you can use the urllib3.HTTPBasicAuthHandler to handle basic authentication.
Import urllib3. Import base64. Credentials =. Authhandler = urllib3.contrib.HTTPBasicAuthHandler(credentials)
Opener = urllib3.buildopener(authhandler) response = opener.open(') print(response.read().decode())
I am using this to log in. Def login(email, password): headers =. data =. r = requests.post(' data=data, headers=headers) return r.text def main(): email = "someone@somewhere.com" password = "somepassword". login = login(email, password). print(login). If name == 'main': main(). Source: How do I use basic auth in requests?
How to pass authorization header in Python?
I'm making a rest call to Azure Mobile Apps from Python.
I need to pass authorization header with my request.
The documentation says that I need to provide the bearer token. But I don't know how to pass it in the request header.
This is what I have done: import requests. Headers =. Response = requests.request("POST", "", headers=headers) print(response). But I get a 403 error, as it says the header is not valid. The same header is working when I do it in the browser. By using your code, I'm able to make the post request successfully on the mobile device. The only difference is I've removed content-type, and set Accept to application/json.
In your case, you've already set the content type and Accept header, so you might need to take out one of these two. Thanks to @Michael and @PaulHoule for the comments. For those who are having a problem passing the header, you should use this format of the header. Instead of using Authorization.
Related Answers
What is the basic format of Authorization header?
I am trying to write my own authorization header....
What are the methods of Web proxy authentication?
A proxy is a program that allows a computer running it to...
How to get basic auth from username and password in Python?
I'm creating REST APIs with angular. I have a login and...