2

I've created an application with asp.net mvc api with users, following this tutorial. And everything works great like that.

Now I'm trying to add external logins (actually just Facebook). I've been looking how to do this and I found an answer here.

So I have the Startup.Auth.cs configured with Facebook AppId/Secret.

I make a call to GET api/Account/ExternalLogins?returnUrl=%2F&generateState=true

And then I redirect to the given Url. and then I'm returned to something like http://localhost:49728/#access_token= ... &token_type=bearer&expires_in=2592000&state= ...

And here is my problem. What should I do next to register the user on the website.

After the web application is complete, there will be a mobile native app.

EDIT
I changed the return url, to where I can ask and save the username. So far good.

After I save the new user, I need to make the login, so I'm trying this:

var state = new RegExp('[\&&]state=([^&#]*)').exec(window.location.href);
                $.get(baseurl + 'api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=' + encodeURIComponent(baseurl) + '&state=' + state[1], function (data, status) {
                    console.log(data);
                });

but I'm getting the 400 Bad Request error.
My question now is how can I fix this, and make the login?

1 Answer 1

4

You need to take the resulting access token and use it to call RegisterExternal web call, by adding it to the authorization header (use postman to test) like this:

Authorization Bearer access token here

in register external web call, you need to provide an email i guess to associate it with the facebook login.

then you need to do external login call again as you did the first time (the link coming from get external logins)

this time the returned access token will be the local access token and not the facebook external access token, this token you can use to call your asp.net api, by adding it to the http requests header as an "Authorization" the same way as i explained.

Edit: for the bad request you are facing try this : go to ApplicationOAuthProvider class, then to ValidateClientRedirectUri method, change that method code to :

 if (context.ClientId == _publicClientId)
 {
    Uri expectedRootUri = new Uri(context.Request.Uri, "/");

    if (context.RedirectUri.StartsWith(expectedRootUri.AbsoluteUri))
    {
       context.Validated();
    }
 }

 return Task.FromResult<object>(null);
Sign up to request clarification or add additional context in comments.

3 Comments

yes, almost done, now the problem is when I try to call again external login, GET /api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A15359%2F&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1 I get 400 error bad request. what is wrong?
i am not sure what is the problem in you case, i need more code, and error logs, it would be more beneficial if you try using postman, and see the error or debug to see the error. however, there is something that i wan t you to try and let me know if it works. i will edit my answer to add that change.
it works now, forgot to set the token

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.