gpt4 book ai didi

wpf - 与 WPF 连接的身份服务器

转载 作者:行者123 更新时间:2023-12-04 18:34:47 26 4
gpt4 key购买 nike

身份服务器客户端:

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

我检查了 token ,是好的 token 。我还启用了 AllowOfflineAccess = true。

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

编辑:网络 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");
}

最佳答案

WPF 应用程序需要异步才能等待来自 api 的响应。

关于wpf - 与 WPF 连接的身份服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50131141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com