locked
Google Authentication [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)] returns UNAUTHORIZED RRS feed

  • Question

  • User-1939321229 posted

    I have a MVC 5 App which is calling a WebAPI.  The WebAPI uses bearer tokens for authentication.  There is one method(action) in the WebAPI which I want to use the token returned by Google for authentication purpose.  In order to do that I do the following to the WebAPI Method

            [OverrideAuthentication]
            [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
            [Route("{userName}")]
            public async Task<User> GetUser(string userName)

    Despite this I still get an UNAUTHORIZED error from the WebAPI.  What do I need to do to fix this?

    Tuesday, June 17, 2014 4:24 AM

Answers

  • User1140095199 posted

    Hi ,

        Greetings!

            [OverrideAuthentication]
            [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
            [Route("{userName}")]
            public async Task<User> GetUser(string userName)

    Do you intent to Override all global settings and allow just External Authentication? Or Do you want to access a Cookie?

    Take a look at the article below:

    Dissecting the Web API Individual Accounts Template–Part 1: Overview

    Also, kindly check and make sure that you have done everything correctly and got the Token. Another things is probably using just Authorize would do the job.

    With the access token, the client is now allowed to call other account management related operations on the account controller (e.g. userInfo, changePassword or setPassword) or any other controllers decorated with the [Authorize] attribute, e.g.:

    private async Task<string> GetUserInfo(string token)
    {
        var client = new HttpClient { BaseAddress = _baseAddress };
        client.SetBearerToken(token);
        var response = await client.GetStringAsync(“api/account/userInfo”);
        return response;
    }
    

    For more reference:

    Dissecting the Web API Individual Accounts Template–Part 2: Local Accounts

    Hope it helps!

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 18, 2014 3:11 AM