使用ASP.NET Identity 3.0的ASP.NET Core 5,我正在使用网页和apis.我正在使用OpenIddict发出JWT令牌并进行身份验证.我的代码看起来像这样:
X509Certificate2 c = new X509Certificate2(@"tokensign.p12", "MyCertificatePassword");
services.AddOpenIddict<WebUser, IdentityRole<int>, WebDbContext, int>()
.EnableTokenEndpoint("/api/customauth/login")
.AllowPasswordFlow()
.UseJsonWebTokens()
.AddSigningCertificate(c);
Run Code Online (Sandbox Code Playgroud)
如果我禁用UseJsonWebTokens(),我可以生成一个令牌并成功授权.但是,我不确定我的证书是否验证了返回的令牌.
当启用UseJsonWebTokens时,我能够在此终点发出JWT令牌.但是,我无法验证任何请求!
我在应用程序配置中使用以下代码:
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
Authority = "http://localhost:60000/",
Audience = "http://localhost:60000/",
});
app.UseOAuthValidation();
app.UseIdentity();
app.UseOpenIddict();
app.UseMvcWithDefaultRoute();
Run Code Online (Sandbox Code Playgroud)
我将 ASP.NET Core 与 OpenIddict、JWT、资源所有者授予和基于声明的角色一起使用。不执行任何策略的授权正在按预期进行。
我想对某些控制器和操作方法强制执行授权策略。我的所有用户都有角色声明,因此我在启动中执行了以下操作:
services.AddAuthorization(options =>
{
options.AddPolicy("Admin", p => p.RequireClaim("Admin");
});
Run Code Online (Sandbox Code Playgroud)
我对操作方法做了以下操作:
[Authorize("Admin")]
public async Task<string> Index()
{
return "Yes";
}
Run Code Online (Sandbox Code Playgroud)
没有“管理员”,我能够访问该资源,添加“管理员”后我不能。
我假设是因为我生成的 JWT 令牌没有用户声明。
我正在使用这个github项目https://github.com/openiddict/openiddict-core 这很棒.但是,当用户使用外部身份提供商时,我会被困在程序应该是什么,或者如何实现它们,对于这个例子,我将使用谷歌.
我有一个angular2 app运行,带有aspnet核心webAPI.我所有的本地登录工作connect/token
都很完美,我使用用户名和密码调用,并返回accessToken.
现在我需要将谷歌作为外部身份提供商实施.我已按照此处的所有步骤实施Google登录按钮.这会在用户登录时打开一个弹出窗口.这是我为google按钮创建的代码.
// Angular hook that allows for interaction with elements inserted by the
// rendering of a view.
ngAfterViewInit() {
// check if the google client id is in the pages meta tags
if (document.querySelector("meta[name='google-signin-client_id']")) {
// Converts the Google login button stub to an actual button.
gapi.signin2.render(
'google-login-button',
{
"onSuccess": this.onGoogleLoginSuccess,
"scope": "profile",
"theme": "dark"
});
}
}
onGoogleLoginSuccess(loggedInUser) {
let idToken = loggedInUser.getAuthResponse().id_token;
// here i can pass the …
Run Code Online (Sandbox Code Playgroud) 我按照 openiddict 服务器示例中的说明使用https://github.com/openiddict/openiddict-samples/tree/master/samples/PasswordFlow中的密码流 但没有成功。
它抛出InvalidOperationException: 无法从此端点在路由/connect/token返回 OpenID Connect 响应:
return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
邮递员参数:
我花了一天时间进行研究,但没有关于无法从此端点异常返回的信息。除了我之外,很多人都可以运行 openiddict 示例。
这是 Startup.cs 的一部分:
services.AddEntityFrameworkSqlite()
.AddDbContext<MisapayContext>(options =>
{
options.UseOpenIddict<int>();
});
//....
services.AddOpenIddict<int>()
.AddEntityFrameworkCoreStores<MisapayContext>()
.DisableHttpsRequirement()
.EnableTokenEndpoint("/connect/token")
.EnableLogoutEndpoint("/connect/logout")
.EnableUserinfoEndpoint("/connect/userinfo")
.UseJsonWebTokens()
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.AddEphemeralSigningKey();
services.AddMvc(config =>
{
config.Filters.Add(new ApiExceptionFilter());
}).AddJsonOptions(options =>
{
options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
});
Run Code Online (Sandbox Code Playgroud)
编辑:我认为问题是OpenIdConnectRequest,如果使用它则无法绑定:
OpenIddictBuiler.AddMvcBinders()
将抛出无法从 ASP.NET 上下文检索 OpenID Connect 请求。
否则,去掉它,AuthorizationController中的OpenIdConnectRequest就可以正常获取。我可以获取请求信息,例如用户名、密码 grantType 等...奇怪...对吧?
其他一些信息:
我正在使用配置为使用json web令牌的openiddict:
// Add authentication
services.AddAuthentication();
// Add OpenId Connect/OAuth2
services.AddOpenIddict()
.AddEntityFrameworkCoreStores<ApplicationDbContext>()
.AddMvcBinders()
.EnableTokenEndpoint("/connect/token")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.UseJsonWebTokens() // access_token should be jwt
// You can disable the HTTPS requirement during development or if behind a reverse proxy
.DisableHttpsRequirement()
// Register a new ephemeral key, that is discarded when the application
// shuts down. Tokens signed using this key are automatically invalidated.
// To be used during development
.AddEphemeralSigningKey();
Run Code Online (Sandbox Code Playgroud)
我通过以下方式配置JWT中间件:
// Add Jwt middleware for authentication
var secretKey = Configuration.Get<AppOptions>().Jwt.SecretKey;
app.UseJwtBearerAuthentication(new …
Run Code Online (Sandbox Code Playgroud) 因此,我尝试使用实现OpenIddict版本1.0.0-beta2-0580
,NET core 1.1
并且出现以下错误:
An unhandled exception occurred while processing the request
这是基于以下内容的:https : //github.com/openiddict/openiddict-core/tree/dev/samples
db正确注册了数据库,设置已加载,并且一切正常。在数据库中的表:__efmigrationshistory
,aspnetroleclaims
,aspnetroles
,aspnetuserclaims
,aspnetuserlogins
,aspnetuserroles
,aspnetusers
,aspnetusertokens
,basetransaction
,openiddictapplications
,openiddictauthorizations
,openiddictscopes
,openiddicttokens
然后我有以下堆栈跟踪:
InvalidOperationException: The authentication ticket was rejected because the mandatory subject claim was missing.
AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerHandler+<HandleSignInAsync>d__5.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Authentication.AuthenticationHandler+<SignInAsync>d__66.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Http.Authentication.Internal.DefaultAuthenticationManager+<SignInAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.SignInResult+<ExecuteResultAsync>d__14.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeResultAsync>d__30.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeNextResultFilterAsync>d__28.MoveNext() …
Run Code Online (Sandbox Code Playgroud) 我是oauth2和openid connect 的新手,所以,许多概念对我来说仍然很模糊 - 我在这里寻求建议
我计划使用以下组件构建一个Web 应用程序:
我拥有三个组件 =身份验证服务器和 SPA 客户端应用程序之间存在完全信任。
我需要用户登录(输入用户名和密码)并直接在 Angular 客户端注册(无需重定向到身份验证服务器注册或登录或权限页面)
我的问题是:
任何答案,任何对这些问题十分赞赏,在此先感谢
在一个旧项目中,我们使用了 ASP.NET 4.5,我想在其中使用框架 OpenIdDict。它是由 ASP.NET Core 1 和 2 制作的。我还能使用它吗?我需要注意什么?如果我不能使用它,你知道哪些替代方案?
OpenIdDict 链接:https : //github.com/openiddict/openiddict-core
我有一个用 angular 7 编写的单页应用程序,它与我的 ASP.Net Core 2.2 Web API 服务器进行通信。登录时,用户使用“资源所有者密码凭据”授权将他的凭据发送到我的授权服务器(连接/令牌)。我正在尝试添加 2 因素身份验证 (SMS),但找不到任何描述如何执行此操作的示例。我发现的所有示例都是使用 MVC 编写的,使用 cookie 身份验证。
我在考虑这个流程,但我觉得应该有更好的方法
asp.net oauth-2.0 two-factor-authentication openiddict angular
我正在使用 openidict 和 oidc-client 身份验证,
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.LoginPath = "/Identity/Account/Login";
options.LogoutPath = "/Identity/Account/Logout";
})
.AddOpenIdConnect(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.ForwardSignIn = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = baseUrl;
options.CallbackPath = new PathString("/authentication/login-callback");
options.SignedOutRedirectUri = baseUrl;
options.ClientId = AuthenticationClient.WebClientId;
options.RequireHttpsMetadata = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.UsePkce = true;
/// Use the authorization code flow.
options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
options.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
options.Scope.Add(Scopes.OpenId);
options.Scope.Add(Scopes.Profile);
options.Scope.Add(AuthenticationClient.WebClientApiScope);
}
Run Code Online (Sandbox Code Playgroud)
此处,当响应类型设置为“代码 id/代码 id_token/代码令牌”时,我收到不支持 Open ID 连接混合流错误。
当它是“代码”时,我收到以下错误。
error:unauthorized_client
error_description:The specified 'response_type' is not …
Run Code Online (Sandbox Code Playgroud) openiddict ×10
asp.net-core ×7
c# ×4
jwt ×3
angular ×2
.net ×1
.net-core ×1
asp.net ×1
oauth ×1
oauth-2.0 ×1
oidc-client ×1
openid ×1