在服务器端,基于角色的Blazor应用授权不起作用

时间:2020-05-14 15:04:55

标签: c# blazor-server-side

我一直在尝试学习Blazor身份服务。但是在应用程序中,“角色”标签不起作用。这是我的Blazor页面

            <AuthorizeView Roles="Admin" Context="adminContext">
                <Authorized>
                    <GridColumn Width="70px">
                        <Template>
                            @{
                                Contact contact = context as Contact;
                                <TelerikButton OnClick="@( e => EditContact(e, contact.ContactId) )">Edit</TelerikButton>
                            }
                        </Template>
                    </GridColumn>
                    <GridColumn Width="90px">
                        <Template>
                            @{
                                Contact contact = context as Contact;
                                <TelerikButton OnClick="@( e => DeleteContact(e, contact.ContactId) )">Delete</TelerikButton>
                            }
                        </Template>
                    </GridColumn>
                </Authorized>
            </AuthorizeView>

我的启动类ConfigureServices方法如下所示。我继承了IdentityRole并创建了自己的名为ApplicationRole的类。如下面的代码所示,我还覆盖了各种商店。同样,我也继承了IdentityUser并创建了ApplicationUser

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddRoles<ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders()
                .AddUserStore<ApplicationUserStore>()
                .AddRoleStore<ApplicationRoleStore>();

    public class ApplicationRoleStore : IRoleStore<ApplicationRole>, IUserRoleStore<ApplicationUserRole>, IQueryableRoleStore<ApplicationRole>
    {
        private readonly ApplicationDbContext _context;


    public class ApplicationUserStore : IUserStore<ApplicationUser>, IUserRoleStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IQueryableUserStore<ApplicationUser>
    {
        private readonly ApplicationDbContext _context;

0 个答案:

没有答案