mo-esmp / DynamicRoleBasedAuthorizationNETCore

Dynamic Role-Based Access Control for ASP.NET Core MVC and Web API
GNU General Public License v3.0
451 stars 94 forks source link

خطا در استفاده از پروژه در MVC Core 5.0 #20

Closed mohammadhosseinzavvar closed 3 years ago

mohammadhosseinzavvar commented 3 years ago

با سلام من طبق آموزش گیت، پکیج ها و نصب کردم و به پروژه اضافه کردم تنظیمات فایل Startup هم قرار دادم ولی در اجرا خطا میده GenericArguments[0], 'SeyedMarket.DomainClasses.Context.ApplicationDbContext', on 'DynamicAuthorization.Mvc.Ui.Services.IdentityService`4[TDbContext,TUser,TRole,TKey]' violates the constraint of type 'TDbContext'.'

mohammadhosseinzavvar commented 3 years ago

`#region تنظیمات Identity services.AddIdentity<ApplicationUser, ApplicationRole>(options => { options.SignIn.RequireConfirmedAccount = false;

            //تنظیمات گذرواژه
            options.Password.RequiredLength = 5;
            options.Password.RequireLowercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredUniqueChars = 0;
            options.Password.RequireDigit = true;

            // تنظیمات خطای ورود
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;

            // تنظیمات نام کاربری
            options.User.AllowedUserNameCharacters =
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
            options.User.RequireUniqueEmail = false;

        })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
        #endregion

        #region  تنظیمات کوکی
        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(2 * 60);
            options.LoginPath = "/Account/Login";
            options.LogoutPath = "/Account/LogOut";
            options.AccessDeniedPath = "/Error/AccessDenied";
            options.SlidingExpiration = true;
        });
        #endregion

        var MvcBuilder = services.AddControllersWithViews();

        services.AddRazorPages();

        #region افزودن Role Manager
        services.AddDynamicAuthorization<ApplicationDbContext>(options => options.DefaultAdminUser = "Admin")
                .AddSqlServerStore(options => options.ConnectionString = CustomExtensions.ServerConnection)
                .AddUi(MvcBuilder);
        #endregion`
mo-esmp commented 3 years ago

سلام لطفا تنظیمات مربوط به Identity رو هم که انجام دادین، بفرستین یا یک ریپازیتوری گیتهاب ایجاد کنید و همون تنظیمات رو تو پروژە تست انجام بدین و بفرسیتن رو گیتهاب و آدرسش رو بهم بدین

mohammadhosseinzavvar commented 3 years ago

من از کلاس های کاستوم شده یوزر و رول استفاده کردم براتون میفرستم

mohammadhosseinzavvar commented 3 years ago

public class ApplicationRole : IdentityRole { public ApplicationRole() { CreationDateTime = DateTime.Now; Unique = Guid.NewGuid(); }

    public string Title { get; set; }

    /// <summary>
    /// تاریخ و زمان ثبت
    /// </summary>
    public DateTime CreationDateTime { get; set; }

    /// <summary>
    /// تاریخ و زمان بروزرسانی
    /// </summary>
    public DateTime? ChangeDateTime { get; set; }

    /// <summary>
    /// کد یکتا
    /// </summary>
    public Guid Unique { get; set; }

    #region Relationship

    public virtual ICollection<IdentityRoleClaim<int>> Claims { get; set; }
    public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
    #endregion
}
mohammadhosseinzavvar commented 3 years ago

public class ApplicationUser : IdentityUser { public ApplicationUser() { SupplierType = (int)EnumSupplierType.None; AutomaticScore = 0; MaximumAllowedDisplay = 100; CurrentDisplay = 0; IsActive = true; CreationDateTime = DateTime.Now; Unique = Guid.NewGuid(); }

    /// <summary>
    /// شیفت کاری
    /// </summary>
    public int? ShiftWorkID { get; set; }

    /// <summary>
    /// نوع تامین کننده
    /// </summary>
    public int SupplierType { get; set; }

    /// <summary>
    /// امتیاز دستی کاربر
    /// </summary>
    public int? ManualScore { get; set; }

    /// <summary>
    /// امتیاز خودکار
    /// </summary>
    public int AutomaticScore { get; set; }

    /// <summary>
    /// نام و نام خانوادگی
    /// </summary>
    public string LastName { get; set; }

    /// <summary>
    /// تاریخ تولد
    /// </summary>
    public DateTime? DateOfBirth { get; set; }

    /// <summary>
    /// عنوان محل کار شخص
    /// </summary>
    public string Title { get; set; }

    /// <summary>
    /// تلفن
    /// </summary>
    public string Phone { get; set; }

    /// <summary>
    /// فکس
    /// </summary>
    public string Fax { get; set; }

    /// <summary>
    /// کد پستی
    /// </summary>
    [MaxLength(10)]
    public string PostalCode { get; set; }

    /// <summary>
    /// منطقه
    /// </summary>
    public int? ProvinceID { get; set; }

    /// <summary>
    /// آدرس
    /// </summary>
    public string Address { get; set; }

    /// <summary>
    /// کد ملی
    /// </summary>
    [MaxLength(10)]
    public string NationalCode { get; set; }

    /// <summary>
    /// کد اقتصادی
    /// </summary>
    [MaxLength(16)]
    public string EconomicCode { get; set; }

    /// <summary>
    /// حداکثر نمایش مجاز
    /// </summary>
    public int MaximumAllowedDisplay { get; set; }

    /// <summary>
    /// مقدار فعلی نمایش
    /// </summary>
    public int CurrentDisplay { get; set; }

    /// <summary>
    /// موقعیت مکانی
    /// </summary>
    [Column(TypeName = "geometry")]
    public Point Postion { get; set; }

    /// <summary>
    /// میزان انحراف از مرکز بر اساس متر
    /// </summary>
    public decimal LocationOffset { get; set; }

    /// <summary>
    /// تصویر شخص
    /// </summary>
    public string Picture { get; set; }

    /// <summary>
    /// آیا اکانت شخص فعال است
    /// </summary>
    public bool? IsActive { get; set; }

    /// <summary>
    /// دلیل از کار انداختن اکانت کاربر
    /// </summary>
    public string DescriptionForDeactivate { get; set; }

    /// <summary>
    /// کد معرف
    /// </summary>
    public string Reagent { get; set; }

    /// <summary>
    /// آی دی سطر ویزیتور این کاربر
    /// </summary>
    public int? VisitorID { get; set; }

    /// <summary>
    /// آی دی پوش
    /// </summary>
    public string PushID { get; set; }

    /// <summary>
    /// استثنا در محاسبه نقطه سفارش
    /// </summary>
    public bool? ExceptionInOrderPoint { get; set; }

    /// <summary>
    /// مانده حساب
    /// </summary>
    public decimal Remaining { get; set; }

    /// <summary>
    /// تاریخ و زمان ثبت
    /// </summary>
    public DateTime CreationDateTime { get; set; }

    /// <summary>
    /// تاریخ و زمان بروزرسانی
    /// </summary>
    public DateTime? ChangeDateTime { get; set; }

    /// <summary>
    /// کد یکتا
    /// </summary>
    public Guid Unique { get; set; }

    #region Relationship

    public virtual ICollection<IdentityUserClaim<int>> Claims { get; set; }
    public virtual ICollection<IdentityUserLogin<int>> Logins { get; set; }
    public virtual ICollection<IdentityUserToken<int>> Tokens { get; set; }
    public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
    public virtual List<TblCart> Carts { get; set; }
    public virtual List<TblDevice> Devices { get; set; }

    /// <summary>
    /// طرف حساب فاکتور
    /// </summary>
    public virtual List<TblFactor> PersonFactors { get; set; }

    /// <summary>
    /// بازاریاب فاکتور
    /// </summary>
    public virtual List<TblFactor> VisitorFactors { get; set; }

    /// <summary>
    /// راننده فاکتور
    /// </summary>
    public virtual List<TblFactor> DriverFactors { get; set; }

    /// <summary>
    /// ارجاعات فاکتور
    /// </summary>
    public virtual List<TblFactorEvent> FactorEvents { get; set; }

    /// <summary>
    /// بازاریاب
    /// </summary>
    public virtual ApplicationUser Visitor { get; set; }

    public virtual List<ApplicationUser> Persons { get; set; }

    public virtual List<TblTrafficLog> TrafficLogs { get; set; }

    public virtual List<AspNetUserType> UserTypes { get; set; }

    public virtual TblShiftWork ShiftWork { get; set; }

    public virtual TblProvince Province { get; set; }

    public virtual List<TblEvent> RegistrarEvents { get; set; }

    public virtual List<TblEvent> ModifierEvents { get; set; }

    public virtual List<TblEvent> Events { get; set; }

    public virtual List<TblVisitDay> VisitDays { get; set; }

    public virtual List<TblProductSupplier> ProductSuppliers { get; set; }

    public virtual List<TblShareHolder> ShareHolders { get; set; }

    #endregion
}
mohammadhosseinzavvar commented 3 years ago

public class ApplicationDbContext : IdentityDbContext< ApplicationUser, ApplicationRole, int, IdentityUserClaim, ApplicationUserRole, IdentityUserLogin, IdentityRoleClaim, IdentityUserToken>

mohammadhosseinzavvar commented 3 years ago

public class ApplicationUserRole : IdentityUserRole { public virtual ApplicationUser User { get; set; } public virtual ApplicationRole Role { get; set; } }

mohammadhosseinzavvar commented 3 years ago

سلام مجدد مشکل قابل حل هست، اطلاعات بیشتری نیاز دارین ؟

mo-esmp commented 3 years ago

فکر کنم با این اطلاعات بتونم خطا رو باز تولید کنم

mohammadhosseinzavvar commented 3 years ago

سلام، مشکل قابل حل هست ؟

mo-esmp commented 3 years ago

بله قابل حل هست، تا فردا درستش میکنم

mo-esmp commented 3 years ago

لطفا این تست رو نگاه کن و ببین با شریط کلاس DbContext شما مطابقت داره یا نه؟ هر چند این کد

public class ApplicationDbContext : IdentityDbContext< ApplicationUser, ApplicationRole, int, IdentityUserClaim, ApplicationUserRole, IdentityUserLogin, IdentityRoleClaim, IdentityUserToken>

کامپایل نمیشه و IdentityRoleClaim, IdentityUserToken, IdentityUserToken رو باید کاستومایز کنید ونوع کلیدشون رو اینتیجر کنید

mohammadhosseinzavvar commented 3 years ago

تمام مدل هام و کاستوم کردم دقیقا مثل این مثال مایکروسافت https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-5.0#add-all-navigation-properties

mohammadhosseinzavvar commented 3 years ago

ولی بازم همون خطای اول و میده 'GenericArguments[0], 'MyApplication.DomainClasses.Context.ApplicationDbContext', on 'DynamicAuthorization.Mvc.Ui.Services.IdentityService`4[TDbContext,TUser,TRole,TKey]' violates the constraint of type 'TDbContext'.'

mo-esmp commented 3 years ago

لطفا نسخه 1.1.0 رو نصب کن و اگه باز باگ داشت بهم خبر بده.

mohammadhosseinzavvar commented 3 years ago

بازهم خطای قبل و میده در زمان اجرا

mo-esmp commented 3 years ago

میشه پروژت رو به no.esmp@gmail.com برام بفرستی

mohammadhosseinzavvar commented 3 years ago

سلام فرستادم، لطفا بررسی بفرمایید براتون رسیده

mo-esmp commented 3 years ago

با عرض معذرت ایمیل رو اشتباه تایپ کردم Mo.esmp@gmail.com به این ایمیل فوروارد کنید

mohammadhosseinzavvar commented 3 years ago

سلام فرستادم.

On Fri, 19 Mar 2021, 05:07 Mohsen Esmailpour, @.***> wrote:

با عرض معذرت ایمیل رو اشتباه تایپ کردم @.*** به این ایمیل فوروارد کنید

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mo-esmp/DynamicRoleBasedAuthorizationNETCore/issues/20#issuecomment-802447782, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMFWYCSIV2UPPD33QBDNMDTTEKTGDANCNFSM4ZDPQEHA .

mo-esmp commented 3 years ago

با عرض معذرت ایمیل رو اشتباه تایپ کردم Mo.esmp@gmail.com به این ایمیل فوروارد کنید

ایمیلی دریافت نکردم، لطفا یکبار دیگه اسپلینگ ایمیل رو چک کنید

mo-esmp commented 3 years ago

لطفا به نسخه 1.1.1 آپدیت کن و تست کن.

mohammadhosseinzavvar commented 3 years ago

این مشکل حل شده ولی UI لود نمیشه http://localhost:50455/role این آدرس خطای 404 میده

mohammadhosseinzavvar commented 3 years ago

سلام روز بخیر مشکل Url حل شد ؟

mo-esmp commented 3 years ago

اگه دارید با همون پروژەای که برای من فرستادین، تست میکنید باید آدرس رو role/index وارد کنید که اونم چون لاگین نیستید 401 برمیگردونه و ریدایرکت میشه به Account/Login?ReturnUrl=%2Frole%2Findex که اون هم چون وجود نداره 404 میده. توصیه میکنم یه پروژه وب از نوع MVC بسازید و نوع Authenticate Type رو Individual Accounts انتخاب کنید و پس از ایجاد یک کاربر به آدرس /role مراجعه کنید و البته یادتون نره اینجا services.AddDynamicAuthorization<ApplicationDbContext>(options => options.DefaultAdminUser = "Admin") اسم کاربری که ساختین رو بزنید options.DefaultAdminUser = "Admin"

mohammadhosseinzavvar commented 3 years ago

http://localhost:50455/Role/Index این ادرس و با یوزر ادمین میزنم این متن خطا و میده InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Internal.InternalDbSet1[ProjectName.DomainClasses.ApplicationRole]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Identity.IdentityRole]'.

کلاس Role من سفارشی شده هست

mo-esmp commented 3 years ago

چک میکنم و بهتون خبر میدم

mo-esmp commented 3 years ago

لطفا به نسخه 1.2.0 آپدیت کن.

mohammadhosseinzavvar commented 3 years ago

سپاس مشکلات حل شد