标签: asp.net-core-identity

需要在 Asp.Net Core 中处理 Post Authenticate

我已准备好使用 Asp.Net 核心,但这就是我正在做的。在 MVC 5 中,我有一个 Http 模块正在处理 PostAuthenticate 事件,以便创建声明,我正在做一些事情来确定用户的角色。我认为无法在 Core 中做同样的事情。请注意,这是使用 Windows 身份验证,因此无需处理登录方法。

从当前连接到 PostAuthenticate 的 httpModule 开始,因为我想为用户初始化一些东西。
context.PostAuthenticateRequest += Context_PostAuthenticateRequest;

请注意,Core 不再存在 httpModules,并且正在将其移至中间件。不过,我不知道如何从那里利用该事件。

asp.net-core asp.net-core-identity

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

ASP.NET Core 2.0.3 ClaimsTransformer 与 HttpContextAccessor 结合使用,声明正在被清除

在我的 Asp.Net Core 应用程序中,我想向我的 ClaimsIdentity 添加自定义声明,以便我可以在应用程序的不同层访问这些声明。为了实现这一点,我添加了以下代码

启动

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddTransient<IPrincipal>(
            provider => provider.GetService<IHttpContextAccessor>().HttpContext.User);
        services.AddTransient<IClaimsTransformation, ClaimsTransformer>();
Run Code Online (Sandbox Code Playgroud)

索赔变压器

public class ClaimsTransformer : IClaimsTransformation
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly IPrincipal _principal;
    public ClaimsTransformer(IUnitOfWork unitOfWork, IPrincipal principal)
    {
        _unitOfWork = unitOfWork;
        _principal = principal;
    }
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        var currentPrincipal  = (ClaimsIdentity)_principal.Identity;
        var identity = (ClaimsIdentity)principal.Identity;
        if (currentPrincipal.Claims.All(p => p.Type != "UserId"))
        {
            var person = _unitOfWork.PersonRepository.GetPersonBySubjectId(principal.Claims.First(p => p.Type == "sub").Value);
            person.Wait();
            if (person.Result != null)
            {
                currentPrincipal.AddClaim(new Claim("UserId", person.Result.Id.ToString())); …
Run Code Online (Sandbox Code Playgroud)

identity claims-based-identity asp.net-core asp.net-core-identity asp.net-core-2.0

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

如何在没有角色的情况下使用 ASP.NET Core Identity?

在没有角色实现的情况下在asp.net core 2中实现身份是否可行?
我试图实现以下内容:

services.AddIdentityCore<TUser>();
Run Code Online (Sandbox Code Playgroud)

但这似乎也不起作用。

c# asp.net-core asp.net-core-identity asp.net-core-2.0

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

IdentityServer4快速入门注册问题

我使用ASP.NET核心标识(基于优秀的快速入门)与IdentityServer4一起使用ASP.NET核心应用程序. http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html 在演练博客中,他们谈到导航到localhost:5000/Account/Register以在Identity db中创建新用户.当我导航到该网址时,我会看到一个白页.此外,我没有Register.cshmtl页面或Register路由或其中包含术语Register的任何内容.

我得错了分店吗?因为我正在发布并使用核心2.0

我是新来的,如果我遗漏了一些明显的东西,我会道歉.我已经运行了dotnet ef命令,但是在我看的任何地方都看不到db - 比如在sql express或LocalDb中.我在端口5000上运行vs17的Identity Server项目

如果我运行MvcClient项目,我会看到带有安全链接的主页.如果我点击我指向IS4实例,但alice和bob登录将工作.(无效我们/ pw).我可以在日志中看到alice和bob用户没有在内存中创建

identityserver4 asp.net-core-identity

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

无法解析 Microsoft.AspNetCore.Identity.RoleManager for ASP.Net Core 2.0 类型的服务

如何在 ASP.Net Core 2.0 中注入 RoleManager?

我收到以下错误:

无法解析 Microsoft.AspNetCore.Identity.RoleManager for ASP.Net Core 2.0 类型的服务

我有以下代码:

  // add identity
  var builder = services.AddIdentityCore<AppUser>(o =>
  {
    // configure identity options
    o.Password.RequireDigit = false;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
    o.Password.RequireNonAlphanumeric = false;
    o.Password.RequiredLength = 6;
  });
  builder = new IdentityBuilder(builder.UserType, typeof(AppRole), builder.Services);
  builder.AddSignInManager<SignInManager<AppUser>>();
Run Code Online (Sandbox Code Playgroud)

AppUser 和 AppRole 分别派生自 IdentityUser 和 IdentityRole。

asp.net-core asp.net-core-identity

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

.Net Core Identity 2提供程序登录取消导致未处理的异常

我已将LinkedIn添加为提供商。我已实现登录并在LinkedIn上注册,没有任何问题。在用例中,用户从提供者页面中取消(链接登录或取消应用程序的授权),身份中间件似乎抛出未处理的异常:

处理请求时发生未处理的异常。
异常:user_cancelled_login; Description =用户取消了LinkedIn登录
未知位置

例外:处理远程登录时遇到错误。

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()

System.Exception:user_cancelled_login; Description =用户取消了LinkedIn登录

例外:处理远程登录时遇到错误。

启动时的提供程序设置定义了回调:

services.AddAuthentication().AddOAuth("LinkedIn", "LinkedIn", c =>
        {
            c.ClientId = Configuration["Authentication:LinkedIn:ClientId"];
            c.ClientSecret = Configuration["Authentication:LinkedIn:ClientSecret"];
            c.Scope.Add("r_basicprofile");
            c.Scope.Add("r_emailaddress");
            c.CallbackPath = "/signin-linkedin";
....
Run Code Online (Sandbox Code Playgroud)

正如我所说的,中间件似乎可以处理所有其他情况,除非用户在LinkedIn页面中取消。LinkedIn的返回URL看起来正确:

HTTPS://本地主机:44372 /登入-LinkedIn误差= user_cancelled_login&ERROR_DESCRIPTION = +的用户取消+ + + LinkedIn登录&状态= CfDJ8MA7iQUuXmhBuZKmyWK9xeAgBBkQvnhf1akLhCIn9bsajCPUf7Wg22oeZBH9jZOIY3YrchMSWZ4dH7NQ1UngLKEuqgU-IHfBglbgJDtS-wc4Z-6DnW66uR0G1ubvNVqatFJHYv17pgqZT98suVkvKgihcJdbNEw7b1ThkuFbn9-5EcYhQ5ln6ImoTgthT8io1DOcCfc_-nBVfOa93a6CpUJTsZc9w93i70jn5dKKXSLntZe0VyRSA0r0PKc5spu5En-0R1rxiLjsjo4dy89PV3A

但是永远都不会进入我的ExternalCallback控制器方法,在该方法中会处理其他类似成功登录/授权的情况?

我想知道这是否对第三方供应商的其他人有用吗?

c# linkedin asp.net-core asp.net-core-identity asp.net-core-2.1

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

每个租户的唯一用户名和电子邮件

我正在使用ASP.NET Core 2.1.

我想覆盖默认的用户创建相关验证机制。

目前我无法使用相同的UserName.

我的ApplicationUser模型有一个名为TenantID.

我想要实现的目标:UserName&EmailAddress必须是每个租户唯一的。

我一直在谷歌上搜索一个解决方案,但没有找到太多asp.net core关于这个的信息。

大多数结果只会涵盖Entity Framework方面,就好像它只是一个覆盖 OnModelCreating(...)方法的问题。有些与 ASP.NET Identity 的非核心版本有关。

我想知道我是否应该继续调查OnModelCreating方法?

或者,还有其他需要覆盖的东西Identity

c# multi-tenant asp.net-core asp.net-core-identity

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

调试 ASP.Net Core 2.1 时自动登录

我正在尝试在我构建的 ASP.net core 2.1 应用程序上自动登录以进行调试。

得到错误:

HttpContext 不能为空。

下面的代码位于Startup.cs文件中

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider ServiceProvider)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseCookiePolicy();

        CreateRoles(ServiceProvider).Wait();

        if (env.IsDevelopment())
        {
            DeveloperLogin(ServiceProvider).Wait();
        }
    }


    private async Task DeveloperLogin(IServiceProvider serviceProvider){

        var UserManager = serviceProvider.GetRequiredService<UserManager<User>>();
        var signInManager = serviceProvider.GetRequiredService<SignInManager<User>>();

        var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc .net-core asp.net-core asp.net-core-identity

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

共享服务器上 PersistKeysToFileSystem 的文件路径

我正在尝试让我的密钥对登录的用户保持不变。由于我目前正在为网站使用共享主机,因此我决定使用文件系统来存储密钥环。所以代码看起来像这样:

services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(""))
.SetApplicationName("MyWebsite")
.SetDefaultKeyLifetime(TimeSpan.FromDays(90))
.ProtectKeysWithCertificate(cert);
Run Code Online (Sandbox Code Playgroud)

然而,我真正不明白的是我应该把这些钥匙放在哪里,以及我要经过什么路径才能让它们在那里。由于这是一个 MVC 核心应用程序,我有点困惑,在 MVC 5 中我会将它放在 App_Data 文件夹中,但这里没有 App_Data 文件夹,我想确保它保持安全并且无法通过浏览器访问。

另一件事是我传递的是相对路径还是直接路径?如果是相对的,我的起点在哪里?它是 bin、根目录还是其他什么?

c# asp.net-mvc asp.net-core-mvc asp.net-core-identity asp.net-core-mvc-2.0

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

仅在具有授权属性的端点上有条件地使用中间件

我已经编写了一个我只想在经过身份验证的端点上运行的中间件。

所以基本上我希望它在我的实现中只在控制器或动作标记为[Authorize]. 任何不需要授权的控制器操作都不需要我的中间件来触发。

我发现了 UseWhen 功能,但我管理的最好的是中间件仅在用户通过身份验证后触发。但是,如果用户登录后仍会在所有端点上触发。

这是我目前的条件。

        app.UseWhen(context => context.User.Identity.IsAuthenticated, appBuilder =>
        {
            appBuilder.UseAutomaticallyRefreshTokenMiddleware();
        });
Run Code Online (Sandbox Code Playgroud)

我想我只需要更改上下文检查,但不确定用什么替换它。

c# middleware asp.net-core asp.net-core-identity

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