首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加身份中断OpenIddict

添加身份中断OpenIddict
EN

Stack Overflow用户
提问于 2019-02-24 00:32:46
回答 1查看 175关注 0票数 0

我在这里遵循OpenIddict示例中的确切示例:https://github.com/openiddict/openiddict-core。在我使用AddIdentity部分之前,一切都正常工作。我真的需要使用身份。注释掉Identity部分会起作用,如果它是未注释的,那么我在测试控制器中的get方法上会得到一个404,因为它不会授权。我使用的是.Net Core2.x

Startup.cs:

代码语言:javascript
复制
public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<XXIdentityContext>(options =>
        {
            options.UseSqlServer(config["default:connectionString"]);
            options.UseOpenIddict();
        });

        services.AddIdentity<AspUser, IdentityRole>()
            .AddEntityFrameworkStores<XXIdentityContext>()
            .AddDefaultTokenProviders();

        services.AddOpenIddict()
            .AddCore(options =>
            {
                options.UseEntityFrameworkCore()
                               .UseDbContext<XXIdentityContext>();
            })

            .AddServer(options =>
            {
                options.UseMvc();
                options.EnableTokenEndpoint("/connect/token");
                options.AllowPasswordFlow();
                options.DisableHttpsRequirement();
                options.AcceptAnonymousClients();
            })
            .AddValidation();
    }

    public void Configure(IApplicationBuilder app,
                          IHostingEnvironment env,
                          ILoggerFactory loggerFactory)
    {
        app.UseAuthentication();
        app.UseMvc();
    }

AspUser.cs:

代码语言:javascript
复制
public class AspUser : IdentityUser
{
}

XXIdentityContext.cs:

代码语言:javascript
复制
public class XXIdentityContext: IdentityDbContext<AspUser>
{
    private IConfiguration config;
    public XXIdentityContext(DbContextOptions<XXIdentityContext> options, IConfiguration config) : base(options)
    {
        this.config = config;
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

TestController.cs:

代码语言:javascript
复制
public class TestController : Controller
{
    [HttpPost("~/connect/token"), Produces("application/json")]
    public IActionResult Exchange(OpenIdConnectRequest request)
    {
        var claims = new List<Claim>
        {
            new Claim(ClaimsConstants.Id, "bob"),
            new Claim(ClaimsConstants.Temp, 5.ToString()),
            new Claim(OpenIdConnectConstants.Claims.Subject, "Testing")
        };

        foreach (var claim in claims)
            claim.SetDestinations(OpenIdConnectConstants.Destinations.AccessToken);

        var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "OpenIddict"));

        return SignIn(principal, OpenIddictServerDefaults.AuthenticationScheme);
    }

    [Authorize, HttpGet("~/api/test")]
    public IActionResult GetMessage()
    {
        return Json(new
        {
            Subject = User.GetClaim(OpenIdConnectConstants.Claims.Subject),
            Id = User.GetClaim(ClaimsConstants.Id),
            Temp= User.GetClaim(ClaimsConstants.Temp)
        });
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-25 12:49:21

在ConfigureServices方法的末尾添加以下代码行解决了此问题:

代码语言:javascript
复制
services.AddAuthentication(o =>
{
     o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54843659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档