标签: asp.net-core-identity

带有自定义存储的 IdentityServer4 Asp.Net Core Identity

我将 IdentityServer4 与 Asp.Net Identity 一起使用。我需要实现一个听起来很简单的自定义身份存储,但是我看到的所有示例都使用了我没有使用的 EntityFramework 核心。

也许还有另一种使用自定义用户存储的方法。任何人都可以指出我使用 IdentityServer4 使用自定义凭证存储的示例。

identityserver4 asp.net-core-identity

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

相当于 ASP.NET Core 2.0 中的 GetExternalAuthenticationTypes()?

我花了很长时间尝试将 ASP.NET 5.0 应用程序转换为 ASP.NET Core 2,但我找不到一些东西。我正在尝试获取当前安装支持的外部提供商(Google、Facebook 等)的列表。Pre Core,我会使用:

Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes()
Run Code Online (Sandbox Code Playgroud)

当然,由于 OWIN 参考,我不能再使用它,但我也无法在其他任何地方找到类似的功能。我不想手动设置它。是否有使用它来创建一个不错的社交登录页面的示例?任何帮助将非常感激!

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

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

如何重定向到 ASP.NET Ideneity 登录页面?

使用 ASP.NET Identity,如果我想构建一个<a>链接到登录页面的元素,我可以使用 Razor Helpers 和一些魔法字符串:

<a asp-area="Identity" asp-page="/Account/Login">Login</a>
Run Code Online (Sandbox Code Playgroud)

(不是手头的问题,但如果有人能告诉我为什么这个复杂的魔法字符串集合比硬编码“/Identity/Account/Login”相对 URL 更好,我会很高兴。)

我的问题是:如果我想从 Handler 例程(即剃刀页面背后的 C# 代码)返回重定向,是否有一些推荐的魔法可以用来获取登录页面的相对 URL?

换句话说,还有什么比:

return Redirect("/Identity/Account/Login");
Run Code Online (Sandbox Code Playgroud)

asp.net-core-identity razor-pages

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

无法通过 UserManager&lt;TUser&gt; 在 asp.net core 2.2 应用程序中使用 ef core 2.2 更新用户

我正在尝试使用更新用户UserManager<TUser>但收到以下错误

无法跟踪实体类型“UserEntity”的实例,因为已跟踪具有键值“{Id}”的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。

互联网上有几个关于上述错误的问题,但在通过UserManager.

这是我的代码:

服务配置:

        services.AddScoped<IUserRepository, UserRepository>();
        services.AddIdentity<UserEntity, UserRoleEntity>()
                    .AddEntityFrameworkStores<GallaContext>()
                    .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

用户实体:

    public class UserEntity : IdentityUser<Guid>
    {
        private ProfileEntity _profile;

        private UserEntity(ILazyLoader lazyLoader)
        {
            LazyLoader = lazyLoader;
        }

        private ILazyLoader LazyLoader { get; set; }

        public UserEntity()
        {

        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Designation { get; set; }
        public string IdentityNumber { get; set; }
        public bool Active { get; set; }
        public …
Run Code Online (Sandbox Code Playgroud)

c# repository-pattern asp.net-core-identity asp.net-core-2.2 ef-core-2.2

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

如何在我的 ASP.NET Core 应用程序中显示和更新我的自定义标识字段?

我使用它创建了一个 ASP.NET Core 应用程序,core 3.1并在其中包含了开箱即用的身份验证。我想添加一些自定义字段供我的用户完成,例如LocationInstagram。按照此处说明,我创建了一个ApplicationUser.cs模型,该模型继承IdentityUser并成功迁移了该模型以将字段添加到我的AspNetUsers表中。到目前为止一切正常。

我想将这些字段添加到我的用户帐户页面。我意识到我看不到用户帐户页面。使用以下说明,我将缺少的帐户页面添加到我的项目中。但是,当我添加缺少的字段时,它声称无法找到它们,尽管整个项目标识都ApplicationUser按照文档中的说明使用。

区域/页面/帐户/管理/Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Profile";
    ViewData["ActivePage"] = ManageNavPages.Index;
}

<h4>@ViewData["Title"]</h4>
<partial name="_StatusMessage" model="Model.StatusMessage" />
<div class="row">
    HEY
    <div class="col-md-6">
        <form id="profile-form" method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Username"></label>
                <input asp-for="Username" class="form-control" disabled />
            </div>
            <div class="form-group">
                <label asp-for="Location"></label>
                <input asp-for="Location" class="form-control" />

            </div>
            <div class="form-group">
                <label asp-for="Instagram"></label>
                <input asp-for="Instagram" class="form-control" …
Run Code Online (Sandbox Code Playgroud)

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

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

SignInManager.IsSignedIn(User)与User.Identity.IsAuthenticated

_LoginPartial.cshtmlAsp.net Core Web应用程序模板提供的默认值如下。

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    <form asp-controller="Account" asp-action="Logout" method="post">
        <ul >
            <li>
                <a asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit">Log out</button>
            </li>
        </ul>
    </form>
}
else
{
    <ul >
        <li><a asp-controller="Account" asp-action="Register">Register</a></li>
        <li><a asp-controller="Account" asp-action="Login">Log in</a></li>
    </ul>
}
Run Code Online (Sandbox Code Playgroud)

而不是使用注入SignInManager.IsSignedIn(User),我们为什么不使用User.Identity.IsAuthenticated它简单得多?有没有我没有注意到的区别?

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

0
推荐指数
1
解决办法
1169
查看次数

ASP.NET Core 2授权属性jwt

请告诉我为什么这段代码不起作用.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            options.RequireHttpsMetadata = false;
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidIssuer = AuthOptions.ISSUER,
                ValidateAudience = true,
                ValidAudience = AuthOptions.AUDIENCE,
                ValidateLifetime …
Run Code Online (Sandbox Code Playgroud)

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

0
推荐指数
1
解决办法
641
查看次数

ASP.NET Core 2.1:Razor Pages - 基于角色的授权不起作用

我的Razor Pages应用程序配置如下.Startup.cs包含:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlite(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<IdentityUser>()
        .AddRoles<IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddAuthorization(options =>
    {
        options.AddPolicy("RequireAdminRole", policy => 
            policy.RequireAuthenticatedUser().RequireRole("Admin"));
    });

    services.AddMvc()
        .AddRazorPagesOptions(options =>
        {
            options.Conventions.AuthorizePage("/About", "RequireAdminRole");
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy(); …
Run Code Online (Sandbox Code Playgroud)

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

0
推荐指数
1
解决办法
2562
查看次数

身份用户管理器DeleteAsync DbUpdateConcurrencyException

我试图通过webapi后面的aspnetcore.identity UserManager删除用户。

    [HttpPost("Delete", Name = "DeleteRoute")]
    [Authorize(Roles = "SuperUser")]
    public async Task<IActionResult> DeleteAsync([FromBody] User user)
    {
        Console.WriteLine("Deleting user: " + user.Id);
        try {
            await _userManager.DeleteAsync(user);
            return Ok();
        } catch(Exception e) {
            return BadRequest(e.Message);
        }

    }
Run Code Online (Sandbox Code Playgroud)

这引发了 DbUpdateConcurrencyException

   Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected)
   at …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core asp.net-core asp.net-core-identity

0
推荐指数
1
解决办法
428
查看次数

身份的方案名称是什么?

假设我使用以下内容:

services.AddIdentity<User, UserRole>()
        .AddEntityFrameworkStores<AppDbContext>();
Run Code Online (Sandbox Code Playgroud)

设置的认证方案名称是什么?我在任何文档中都找不到。我试图与名称寻找类IdentityAuthenticationDefaultsIdentityDefaults却一无所获。我尝试过“ Cookies”,但未设置为此。该应用程序运行良好,因此肯定设置了一些方案名称。

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

0
推荐指数
1
解决办法
252
查看次数