Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity Server connecting with WPF

Identity Server Client:

    //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 app:

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

I am trying to connect the identity server with wpf app but i always get back a 401.

Identity server is running on : http://localhost:5000/ WPF: http://127.0.0.1/sample-wpf-app

I check the token and is the good one. I also enable AllowOfflineAccess = true.

Why do i always get that error?

Edit: 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");
        }
like image 603
Alexandra Damaschin Avatar asked Dec 18 '25 22:12

Alexandra Damaschin


1 Answers

WPF app need to be async in order to wait for the answer from api.

like image 129
Alexandra Damaschin Avatar answered Dec 24 '25 11:12

Alexandra Damaschin