Wpf-приложение, использующее Web API -Identity Server 4 - PullRequest
0 голосов
/ 25 апреля 2018

WPF OidcClient:

var options = new 
    {
    //redirect to identity server
    Authority = "http://localhost:5000/",
    ClientId = "native.code",
    Scope = "openid profile email fiver_auth_api",
    //redirect back to app if auth success
    RedirectUri = "http://127.0.0.1/sample-wpf-app",
    ResponseMode = OidcClientOptions.AuthorizeResponseMode.FormPost, 
    Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
    Browser = new WpfEmbeddedBrowser()
};

WPF, вызывающий веб-API:

private void CallButtonAsync()
    {
        var accessToken = token;

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

        //on button click call Web api Get movies
        //Initialize HTTP Client 
        client.BaseAddress = new Uri("http://localhost:5001");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        try
        {
            HttpResponseMessage response = client.GetAsync("/movies/get").Result;
            MessageBox.Show(response.Content.ReadAsStringAsync().Result);
        }
        catch (Exception)
        {
            MessageBox.Show("Movies not Found");
        }

    }

Identity Server - WPF, объявленный в качестве клиента // настольное приложение wpf

new Client
{
    ClientId = "native.code",
    ClientName = "Native Client (Code with PKCE)",

    RedirectUris = { "http://127.0.0.1/sample-wpf-app" },
    PostLogoutRedirectUris = { "http://localhost:5000" },

    RequireClientSecret = false,
    RequireConsent = true,

    AllowedGrantTypes = GrantTypes.Code,
    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        IdentityServerConstants.StandardScopes.OfflineAccess,
        "fiver_auth_api"
    },
    AlwaysIncludeUserClaimsInIdToken = true,
    IdentityTokenLifetime=3600,

}

Веб API может использоваться только тогда, когда пользователь авторизуется.Я получил токен в приложении wpf, но при вызове веб-интерфейса я получил 401 (неавторизовано).

Что мне не хватает?

1 Ответ

0 голосов
/ 26 апреля 2018

Мое лучшее, что вы пропускаете

AllowOfflineAccess = true

при определении Клиента на IdentityServer

...