0

I am trying to implement facebook-login for my MVC+Angular site. When user click "Login by FB" I got the callback code, user email, name and etc. After that, I just want to LOGIN user:

HttpContext.GetOwinContext().Authentication.SignIn (new AuthenticationProperties { IsPersistent = true }, identity);

But even if I put this on the next line:

var userId = HttpContext.User.Identity.GetUserId();

userId always is null... What can I do?

UPD1: What I'm trying to say. For "simple" authorization I use this code (in Angular):

        var config = {
        method: 'POST',
        url: '/getAuthToken',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: 'grant_type=password&username=' + email + '&password=' + password
    };

    $http(config)
        .success(function(data) {
            console.log('We`re now authenticated');

In my case, we don't have password, but already have token (thanks to external login). Should I call OWIN (/getAuthToken) with some other parameter (token instead password)?

TimeCoder
  • 175
  • 1
  • 8
  • in the current context is null because the request is Anonymous, the next request will have that information, this answer have wide explanation about it http://stackoverflow.com/questions/33951881/user-identity-getuserid-returns-null-after-successful-login – Byron Oct 03 '16 at 23:42
  • Unfortunately, the next request gives the same result. Here: var userId = HttpContext.Current.User.Identity.GetUserId(); - userId is null. Maybe I have to do anything else in addition to SignIn() for user authentication? – TimeCoder Oct 04 '16 at 06:21
  • You can also get the login process out of the SPA by following the out of the box MVC template that has Facebook login already configured, the user will click Login, it will redirect to Facebook and after the user provides his consent, he will be redirected back to the callback URL in your application and then you can redirect to the path that has the angular app – Haitham Shaddad Oct 04 '16 at 06:24
  • Yes, I know about external logins out of the box, but in this SPA it doesn't work (not my project), and I've implemented OAuth2 myself. I mean, I successfully got all user information from Facebook, but I can't login this user into the site. – TimeCoder Oct 04 '16 at 06:42

0 Answers0