标签: aspnet-contrib

在ASP.NET 5中将声明与OpenIdConnect.Server一起使用

在过去的7天中,我尝试使用OpenIdConnect.Server资源所有者流来设置ASP.NET 5 WebApi 。

我或多或少成功地生成了令牌并访问了[Authorize]受保护的操作。

但是,当我尝试访问时this.User.Identity.Claims,它是空的。我现在正在使用ASP.NET 5,beta6(在升级到最新的beta7并等待其正式发布时遇到了麻烦)

在Startup.cs中,我得到了以下内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCaching();

    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<AuthContext>(options =>
        {
            options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
        });

    services.AddIdentity<AuthUser, AuthRole>(
        options => options.User = new Microsoft.AspNet.Identity.UserOptions
        {
            RequireUniqueEmail = true,
            UserNameValidationRegex = "^[a-zA-Z0-9@_\\.-]+$"
        })
        .AddEntityFrameworkStores<AuthContext, Guid>()
        .AddDefaultTokenProviders();

    services.ConfigureCors(configure =>
    {
        configure.AddPolicy("CorsPolicy", builder =>
        {
            builder.WithOrigins("http:/localhost/", "http://win2012.bludev.com/");
        });
    });

    services.AddScoped<IAuthRepository, AuthRepository>();
}

    public void Configure(IApplicationBuilder app)
    {
        var factory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
        factory.AddConsole();

        app.UseStaticFiles();

        app.UseOAuthBearerAuthentication(options =>
        {
            options.Authority = "http://win2012.bludev.com/api/auth/";
            options.Audience = "http://win2012.bludev.com/"; …
Run Code Online (Sandbox Code Playgroud)

c# claims-based-identity openid-connect aspnet-contrib asp.net-core

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

具有ASP.NET身份3的JWT承载令牌

基于Shaun Luttin在/sf/answers/2160026711/上的一个很好的例子,我能够使用该代码生成和使用承载令牌.微小的变化是获得最新的包:

"dependencies": {
  "Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-rc1-final",
  "AspNet.Security.OpenIdConnect.Server": "1.0.0-beta4"
}
Run Code Online (Sandbox Code Playgroud)

虽然代码是一个很好的开始,但它并不是一个完全集成w/ASP.NET身份的完整解决方案.我修改了AuthorizationProvider类,如下所示:

public override Task GrantResourceOwnerCredentials(
    GrantResourceOwnerCredentialsContext context)
{
    var user = _userManager.FindByNameAsync(context.UserName).Result;
    if (user == null)
    {
        context.Rejected("The user name or password is incorrect.");
    }
    else
    {
        var signInManager = context.HttpContext.RequestServices
            .GetRequiredService<SignInManager<ApplicationUser>>();

        if (signInManager.CanSignInAsync(user).Result &&
            _userManager.CheckPasswordAsync(user, context.Password).Result)
        {
            var principal = signInManager.CreateUserPrincipalAsync(user).Result;

            //To avoid leaking confidential data, AspNet.Security.OpenIdConnect.Server
            //refuses to serialize the claims that don't explicitly specify a destination.
            foreach (var claim in principal.Claims)
                claim.WithDestination("token id_token");

            context.Validated(principal);
        }
        else …
Run Code Online (Sandbox Code Playgroud)

bearer-token openid-connect asp.net-identity-3 aspnet-contrib asp.net-core

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

覆盖AspNet.Security.OpenIdConnect.Server中的TokenEndPoint

与此帖子相关的问题:配置授权服务器端点.

使用上面的例子,我可以得到令牌.以前可以通过骑马获得额外的信息

public override Task TokenEndpoint(OAuthTokenEndpointContext context)
        {
            foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
            {
                context.AdditionalResponseParameters.Add(property.Key, property.Value);
            }

            return Task.FromResult<object>(null);
        }
Run Code Online (Sandbox Code Playgroud)

你如何在当前的实施中实现这一目标

public override Task TokenEndpoint(TokenEndpointContext context){
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

c# openid openid-connect aspnet-contrib asp.net-core

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

通过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
查看次数

ASP.NET Core OpenIdConnectServer数据保护密钥位置

你好,

在我的ASP.NET Core应用程序中,我正在使用OpenIdConnectServer进行Api身份验证。一切正常。

但是我无法解决一件事-如何设置自定义文件夹以保留令牌签名密钥?

在服务配置中,我有:

services.AddDataProtection()
        .PersistKeysToFileSystem(new DirectoryInfo(@"keys/"));
Run Code Online (Sandbox Code Playgroud)

首次运行后,将在此处创建一个应用程序密钥。但是OpenIdServer可以自行管理密钥。

app.UseOpenIdConnectServer(options => {
    ...
    options.DataProtectionProvider = app.ApplicationServices.GetDataProtectionProvider();
    ...
});
Run Code Online (Sandbox Code Playgroud)

尽管如此,凭据签名密钥还是在默认位置创建的:

 A new RSA key ... persisted on the disk: /home/.../.aspnet/aspnet-contrib/oidc-server/<some guid>.key.
Run Code Online (Sandbox Code Playgroud)

那是错误还是功能?如何强制服务器将密钥也存储在keys/文件夹中?


摘要-为什么要这样做

我的想法是从n个 docker镜像构建一个API ,将其隐藏在负载平衡器之后,然后在云中运行。问题是-当docker中的每个实例创建其自己的应用程序和签名密钥时,加密的auth令牌将不适用于除已用其密钥创建并签名令牌的其他实例以外的任何其他实例。因此,我试图将相同的密钥分配给每个运行的docker映像。如果可能,到预定义的应用程序文件夹。

还是有更好的方法或最佳做法?


预先谢谢你。

c# openid-connect aspnet-contrib asp.net-core asp.net-core-1.0

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

如何指定现有的ClaimsIdentity的目的地?

我正在使用下面的代码在OpenIdConnectServerProvider.AuthorizationProvider中创建一个ClaimIdentity.但是identity.Name不是searlized.如何让OpenIdConnectServer serarlize这个名字?谢谢.

上一个问题在这里如何在asp.net 5中创建一个ClaimIdentity

var user = await userManager.FindByNameAsync(context.UserName);
var factory = context.HttpContext.RequestServices.GetRequiredService<IUserClaimsPrincipalFactory<ApplicationUser>>();
var identity = await factory.CreateAsync(user);                
context.Validated(new ClaimsPrincipal(identity));       
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-identity openid-connect aspnet-contrib

4
推荐指数
1
解决办法
2344
查看次数

AspNet.Security.OpenIdConnect.Server。刷新令牌

据我所知,ASOS 支持开箱即用的刷新令牌。要获取刷新令牌,我需要向offline_access令牌请求添加范围。但是它们存储在哪里?如何更改令牌的到期日期或将其删除?如何确定为哪个用户刷新令牌创建?

c# oauth-2.0 owin aspnet-contrib asp.net-core

4
推荐指数
1
解决办法
2008
查看次数

ASOS-当具有单独的授权服务器和资源服务器时,令牌验证不起作用

我试图通过此帖子用ASOS强加OpenID Connect服务器(资源所有者密码凭据授予)。当我在一个应用程序中同时拥有授权服务器和资源服务器时,一切工作正常。但是,当我在两个应用程序(但在一台机器上)上拆分它们时,资源服务器无法验证令牌并返回访问令牌无效

我下载了的源代码AspNet.Security.OAuth.Validation以调查此问题,并在此处返回null

以下是来自授权服务器的一些日志:

    信息:Microsoft.AspNetCore.Hosting.Internal.WebHost [1]
          请求启动HTTP / 1.1 POST http:// localhost:5000 / connect / token application / x-www-form-urlencoded; 字符集= UTF-8 77
    信息:AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware [0]
          令牌请求已成功从HTTP请求中提取:{
            “ grant_type”:“密码”,
            “ username”:“ UserLogin”,
            “ password”:“ [出于安全原因被删除]”,
            “ scope”:“ offline_access”
          }。
    信息:AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware [0]
          令牌请求已成功验证。
    记录:AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware [0]
          触发了登录操作:子:123,用户名:UserLogin; [.scopes,[“电子邮件”,“个人资料”,“离线访问”]],[。resources,[“ resource_server”]]。
    dbug:Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository [37]
          从文件'C:\ Users \ User1 \ AppData \ Local \ ASP.NET \ DataProtection-Keys \ key-********-****-****-****-中读取数据64bb57db1c3b.xml”。
    dbug:Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager [18]
          找到了密钥{********-****-****-****-64bb57db1c3b}。
    dbug:Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver [13]
          将失效日期为2017-09-27 16:44:49Z的密钥{********-****-****-****-****-64bb57db1c3b}作为默认密钥。
    dbug:Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor [51]
          使用Windows …

oauth-2.0 openid-connect aspnet-contrib asp.net-core

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