标签: openiddict

通过JWT Token授权

使用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)
  • 如何强制使用我的证书验证请求,以确保JWT令牌未被篡改.
  • 什么是允许验证和授权我的JWT令牌的正确设置,因为如果我不使用JWT,我将获得成功授权.

jwt openid-connect aspnet-contrib asp.net-core openiddict

5
推荐指数
1
解决办法
1859
查看次数

使用 OpenId Connect 进行基于声明的身份验证

我将 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 令牌没有用户声明。

  • 我的 JWT 是否应该包含用户角色声明才能使令牌发挥作用?
  • 如何使用 OpenIddict 发送角色声明?

jwt openid-connect aspnet-contrib asp.net-core openiddict

5
推荐指数
1
解决办法
1111
查看次数

将google idToken替换为本地openId令牌c#

我正在使用这个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)

c# openid oauth asp.net-core openiddict

5
推荐指数
1
解决办法
1605
查看次数

ASP.NET Core Openiddict 抛出“无法从此端点返回 OpenID Connect 响应”

我按照 openiddict 服务器示例中的说明使用https://github.com/openiddict/openiddict-samples/tree/master/samples/PasswordFlow中的密码流 但没有成功。

它抛出InvalidOperationException: 无法从此端点在路由/connect/token返回 OpenID Connect 响应

return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);

邮递员参数:

  • 内容类型: application/x-www-form-urlencoded
  • 参数:用户名=..&密码=...&grantType=密码&范围=offline_access+个人资料+电子邮件

我花了一天时间进行研究,但没有关于无法从此端点异常返回的信息。除了我之外,很多人都可以运行 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 等...奇怪...对吧?

其他一些信息:

  • Asp.net核心SDK 1.1 …

asp.net-core openiddict

5
推荐指数
1
解决办法
3836
查看次数

.net core - 忽略Jwt中间件身份验证签名密钥

我正在使用配置为使用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)

.net c# jwt openid-connect openiddict

5
推荐指数
1
解决办法
3711
查看次数

在OpenIddict中处理请求时发生未处理的异常

因此,我尝试使用实现OpenIddict版本1.0.0-beta2-0580NET core 1.1并且出现以下错误:

An unhandled exception occurred while processing the request

这是基于以下内容的:https : //github.com/openiddict/openiddict-core/tree/dev/samples

连接休息电话

db正确注册了数据库,设置已加载,并且一切正常。在数据库中的表:__efmigrationshistoryaspnetroleclaimsaspnetrolesaspnetuserclaimsaspnetuserloginsaspnetuserrolesaspnetusersaspnetusertokensbasetransactionopeniddictapplicationsopeniddictauthorizationsopeniddictscopesopeniddicttokens

然后我有以下堆栈跟踪:

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)

c# .net-core asp.net-core openiddict

5
推荐指数
1
解决办法
2万
查看次数

openiddict、angular、要使用的流类型(没有身份验证服务器重定向页面)

我是oauth2openid connect 的新手,所以,许多概念对我来说仍然很模糊 - 我在这里寻求建议

我计划使用以下组件构建一个Web 应用程序

  1. 角度的SPA
  2. 带有 asp.net core 2.x 的web API
  3. 带有openiddict 2.x 的身份验证服务器(单独的 - 不在 Web API 的同一个项目中)

我拥有三个组件 =身份验证服务器和 SPA 客户端应用程序之间存在完全信任

我需要用户登录(输入用户名和密码)并直接在 Angular 客户端注册(无需重定向到身份验证服务器注册或登录或权限页面)

我的问题是:

  1. 在这种情况下使用的适当流程是什么?
  2. 关于 angular 客户端:与 angular 客户端一起使用的最佳身份验证包或库是什么,它与身份验证服务器中的 openiddict最兼容且易于使用
  3. 我需要有关应用程序工作流程的建议(在 3 个组件之间)
  4. 是否有针对此堆栈的教程(比示例更好)?

任何答案,任何对这些问题十分赞赏,在此先感谢

asp.net-core openiddict asp.net-core-2.0 angular

5
推荐指数
0
解决办法
277
查看次数

ASP.NET 4.5 或替代框架中的 OpenIdDict?

在一个旧项目中,我们使用了 ASP.NET 4.5,我想在其中使用框架 OpenIdDict。它是由 ASP.NET Core 1 和 2 制作的。我还能使用它吗?我需要注意什么?如果我不能使用它,你知道哪些替代方案?

OpenIdDict 链接:https : //github.com/openiddict/openiddict-core

c# asp.net-mvc-4 aspnet-contrib asp.net-core openiddict

5
推荐指数
1
解决办法
722
查看次数

Angular &amp; Asp.Net Core - 启用 2 因素身份验证

我有一个用 angular 7 编写的单页应用程序,它与我的 ASP.Net Core 2.2 Web API 服务器进行通信。登录时,用户使用“资源所有者密码凭据”授权将他的凭据发送到我的授权服务器(连接/令牌)。我正在尝试添加 2 因素身份验证 (SMS),但找不到任何描述如何执行此操作的示例。我发现的所有示例都是使用 MVC 编写的,使用 cookie 身份验证。

我在考虑这个流程,但我觉得应该有更好的方法

  1. 用户输入他的用户名和密码
  2. 如果用户启用了 2 个因素,我将向他发送带有代码的短信。此外,将向客户端发送有限的 access_token 和 id 令牌。此 access_token 仅在使用户能够发送 2 因素代码时有效。如果 id-token 有“两个因素”的声明:“on”,我会将用户重定向到 SMS 确认代码。
  3. 用户将发送带有代码的 post 请求。如果代码匹配,我将向客户端返回一个包含所有声明的新 access_token。

asp.net oauth-2.0 two-factor-authentication openiddict angular

5
推荐指数
1
解决办法
835
查看次数

响应类型无效

我正在使用 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 oidc-client

5
推荐指数
1
解决办法
2158
查看次数