首页 文章

Identity Server与WPF连接

提问于
浏览
2

Identity Server客户端:

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

                RedirectUris = { "http://127.0.0.1/sample-wpf-app" },
                //PostLogoutRedirectUris = { "https://notused" },

                RequireClientSecret = false,

                AllowedGrantTypes = GrantTypes.Code,
                AllowAccessTokensViaBrowser = true,
                RequirePkce = true,
                AllowedScopes =
                    {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.OfflineAccess,
                    "fiver_auth_api"
                    },


                AllowOfflineAccess = true,
                //Access token life time is 7200 seconds (2 hour)
                AccessTokenLifetime = 7200,
                //Identity token life time is 7200 seconds (2 hour)
                IdentityTokenLifetime = 7200,
                RefreshTokenUsage = TokenUsage.ReUse

                }

WPF应用:

var options = new OidcClientOptions()
        {
            //redirect to identity server
            Authority = "http://localhost:5000/",
            ClientId = "native.code",
            Scope = "openid profile offline_access 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应用程序连接,但我总是回到401 .

身份服务器正在运行:http://localhost:5000/ WPF:http://127.0.0.1/sample-wpf-app

我检查令牌并且是好的 . 我还启用了AllowOfflineAccess = true .

为什么我总是得到那个错误?

编辑:Web Api:

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");
        }

1 回答

  • 0

    为了等待api的回答,WPF app需要异步 .

相关问题