gpt4 book ai didi

c# - 无法在身份服务器 3 中获取 token

转载 作者:行者123 更新时间:2023-11-30 17:31:21 25 4
gpt4 key购买 nike

我是身份服务器 3 的新手。

我可以在其他项目中访问我的 token ,但我无法在我的身份项目中获取它。

这是我的客户类

new Client
{
Enabled = true,
ClientName = "Web Application1",
ClientId = "WebApplication1",
Flow = Flows.Implicit,
RequireConsent = false,
AllowRememberConsent = true,
RedirectUris = new List<string>
{
"http://localhost:52249/"
},
PostLogoutRedirectUris = new List<string>
{
"http://localhost:52249/"
},
IdentityTokenLifetime = 360,
AccessTokenLifetime = 3600,
AllowedScopes = new List<string>() { "openid", "profile" , "roles", "WebAPI" }
},
new Client
{
Enabled = true,
ClientName = "Identity 2",
ClientId = "Identity",
Flow = Flows.Implicit,
RequireConsent = false,
AllowRememberConsent = true,
RedirectUris = new List<string>
{
"https://localhost:44396/"
},
PostLogoutRedirectUris = new List<string>
{
"https://localhost:44396/"
},
IdentityTokenLifetime = 360,
AccessTokenLifetime = 3600,
AllowedScopes = new List<string>() { "openid", "profile" , "roles", "WebAPI" }
},

和我的范围类:

var scopes = new List<Scope>
{
new Scope
{
Enabled = true,
Name = "roles",
Type = ScopeType.Identity,
Claims = new List<ScopeClaim>
{
new ScopeClaim("role")
}
},
new Scope
{
Enabled = true,
DisplayName = "WebAPI",
Name = "WebAPI",
Description = "Secure WebAPI",
Type = ScopeType.Resource,
Claims = new List<ScopeClaim>
{
new ScopeClaim(Constants.ClaimTypes.Name),
new ScopeClaim(Constants.ClaimTypes.Role),
}
}};

scopes.AddRange(StandardScopes.All);
return scopes;
}}

和用户类:

IdentityContext db = new IdentityContext();
var AllUsers = db.Users.ToList();
List<InMemoryUser> UsersList = new List<InMemoryUser>();
foreach (var item in AllUsers)
{
InMemoryUser UserInMemory = new InMemoryUser();
UserInMemory.Username = item.UserName;
UserInMemory.Password = item.Password;
UserInMemory.Subject = item.Id.ToString();
UserInMemory.Claims = new Claim[]
{
new Claim(Constants.ClaimTypes.PreferredUserName, item.UserName),
};
}
return UsersList;

以及我的身份项目中的启动类:

public void Configuration(IAppBuilder app)
{
app.Map("/identity", idsrvApp =>
{
var factory =
new IdentityServerServiceFactory().UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get());

var userService = new UserService();

factory.UserService = new Registration<IUserService>(reslove => userService);
var viewOptions = new DefaultViewServiceOptions();
//viewOptions.Stylesheets.Add("/Content/Site.css");
viewOptions.Stylesheets.Add("/Content/bootstrap-rtl.css");
viewOptions.CacheViews = false;
factory.ConfigureDefaultViewService(viewOptions);
var options = new IdentityServerOptions
{
SiteName = "IdentityServer3 - Configuring DefaultViewService",

SigningCertificate = LoadCertificate(),
Factory = factory,

AuthenticationOptions = new IdentityServer3.Core.Configuration.AuthenticationOptions
{
// IdentityProviders = ConfigureAdditionalIdentityProviders,
}
};

idsrvApp.UseIdentityServer(options);
});

Serilog.Log.Logger =
new LoggerConfiguration().MinimumLevel.Debug()
.WriteTo.RollingFile(pathFormat: @"c:\logs\IdSvrAdmin-{Date}.log")
.CreateLogger();
}

X509Certificate2 LoadCertificate()
{
return new X509Certificate2(
$"{AppDomain.CurrentDomain.BaseDirectory}\\bin\\idsrv3test.pfx", "idsrv3test");
}

请帮帮我。谢谢!

最佳答案

我认为这很有帮助,您可以找到解决此问题的想法。

public HttpRequestWrapper TokenizeRequest(User user, string clientid)
{
var token = GetToken(user, clientid).Result;
_request.AddHeader("Authorization", $"Bearer {token.AccessToken}");
return this;
}

private async Task<TokenResponse> GetToken(User user, string clientid)
{
var client = new TokenClient(Constants.TokenEndpoint, clientid, SecretApi);

return client.RequestResourceOwnerPasswordAsync
(user.UserName, user.Password, "WebAPI").Result;
}

引用: https://www.codeproject.com/Articles/1163131/IdentityServer-WebAPI-MVC-ASP-NET-Identity-Specflo

关于c# - 无法在身份服务器 3 中获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057907/

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