اشتراک‌ها
پیاده‌سازی فرآیند Impersonation در ASP.NET Core

Impersonation Process

Impersonation is when an admin user is logged in with the same privileges as a user, but without knowing their password or other credentials. I’ve used this in couple applications and it was invaluable for support cases and debugging user permissions. 

[Authorize(Roles = "Admin")] // <-- Make sure only admins can access this 
public async Task<IActionResult> ImpersonateUser(String userId)
{
    var currentUserId = User.GetUserId();

    var impersonatedUser = await _userManager.FindByIdAsync(userId);

    var userPrincipal = await _signInManager.CreateUserPrincipalAsync(impersonatedUser);

    userPrincipal.Identities.First().AddClaim(new Claim("OriginalUserId", currentUserId));
    userPrincipal.Identities.First().AddClaim(new Claim("IsImpersonating", "true"));

    // sign out the current user
    await _signInManager.SignOutAsync();

    await HttpContext.Authentication.SignInAsync(cookieOptions.ApplicationCookieAuthenticationScheme, userPrincipal);

    return RedirectToAction("Index", "Home");
}


پیاده‌سازی فرآیند Impersonation در ASP.NET Core
اشتراک‌ها
ویژگی‌های ES2021

ES2021 Features!

Logical Assignment Operators (&&= ||= ??=)
Numeric Separators (1_000)
Promise.any & AggregateError
String.prototype.replaceAll
WeakRefs & FinalizationRegistry Objects
  

ویژگی‌های ES2021
نظرات مطالب
اعمال تزریق وابستگی‌ها به مثال رسمی ASP.NET Identity
سلام با کد ذیل _userStore واسه من نال بر میگردونه
using System.Data.Entity;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity.EntityFramework;
using SmartMarket.Core.Domain.Members;
using SmartMarket.Data;

namespace SmartMarket.Services.Members
{
    /// <summary>
    /// The ApplicationUserStore Class 
    /// </summary>
    public class ApplicationUserStore : UserStore<User, Role, int, UserLogin, UserRole, UserClaim>, IApplicationUserStore
    {
#region Fields (1) 

        private readonly IDbSet<User> _userStore;

#endregion Fields 

#region Constructors (2) 

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationUserStore" /> class.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        public ApplicationUserStore(DbContext dbContext) : base(dbContext) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationUserStore"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public ApplicationUserStore(IdentityDbContext context)
            : base(context)
        {
            _userStore = context.Set<User>();
 
        }

#endregion Constructors 

#region Methods (2) 

// Public Methods (2) 

        /// <summary>
        /// Adds to previous passwords asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public Task AddToPreviousPasswordsAsync(User user, string password)
        {
            user.PreviousUserPasswords.Add(new PreviousPassword { UserId = user.Id, PasswordHash = password });
            return UpdateAsync(user);
        }

        /// <summary>
        /// Finds the by identifier asynchronous.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public override Task<User> FindByIdAsync(int userId)
        {
            return Task.FromResult(_userStore.Find(userId));
        }

#endregion Methods 

        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public override async Task CreateAsync(User user)
        {
            await base.CreateAsync(user);
            await AddToPreviousPasswordsAsync(user, user.PasswordHash);
        }
    }
}
نظرات مطالب
پیدا کردن آیتم‌های تکراری در یک لیست به کمک LINQ
دو سوال:
1) چرا پراپرتی Id در محاسبه هش کد شرکت داده نشده است؟
2) می‌توانستیم ب جای عملگر ^ از عملگر + و یا سایر عملگر استفاده کنیم (به نحوی که هش کد دو شیء یکسان، برابر باشد)؟
نظرات مطالب
نحوه استفاده از ViewModel در ASP.NET MVC
انتساب از طریق عملگر مساوی انجام میشه. مثلا user.Name = userViewModel.Name