首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Web服务器4的Wpf应用程序

使用Web服务器4的Wpf应用程序
EN

Stack Overflow用户
提问于 2018-04-25 21:44:09
回答 1查看 1.7K关注 0票数 2

WPF OidcClient:

代码语言:javascript
复制
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调用web api:

代码语言:javascript
复制
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桌面应用程序

代码语言:javascript
复制
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,

}

仅当用户被授权时,才能使用web api。我在wpf应用程序中获得了令牌,但当我调用web api时,我得到了一个401(未授权)。

我遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2018-04-27 04:45:24

我最棒的是你错过了

AllowOfflineAccess = true

defining the Client at the IdentityServer

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50023955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档