为什么@ User.IsInRole不工作? [英] Why @User.IsInRole not working ?

查看:74
本文介绍了为什么@ User.IsInRole不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我安装了Dotconnect PostgreSql以及本教程https://www.devart.com/dotconnect/postgresql/articles/aspproviders.html#role

一切正常,但是当我使用此操作从View.cshtml调用Admin角色时:@ User.IsInRole(admin){},之后显示错误为操作不被支持。

但是在web.config中启用了Rolemanager



显示异常:



Devart.Data.PostgreSql.Web.dll中出现'System.InvalidOperationException'类型的异常,但未在用户代码中处理



附加信息:操作是不支持





如何解决?



谢谢为你的答案!

Hi ,

I installed Dotconnect PostgreSql with folowing this tutorial https://www.devart.com/dotconnect/postgresql/articles/aspproviders.html#role
All works fine, but when I call Admin role from View.cshtml with this operation : @User.IsInRole("admin") { } , after that showing error as "Operation is not Suppoorted".
But Rolemanager was enabled in web.config

Showing Exception that :

An exception of type 'System.InvalidOperationException' occurred in Devart.Data.PostgreSql.Web.dll but was not handled in user code

Additional information: Operation is not supported


How can I fix it ?

Thank you for your answer!

推荐答案

如果我没有遗漏任何VB服务,在 Microsoft.VisualBasic.ApplicationServices [ ^ ],而不是MVC。然而它确实像这样执行, User.IsInRole(string) [ ^ ]。



您可以考虑使用Authorize属性并将角色传递给它,



If I am not missing anything that is a VB service, defined under Microsoft.VisualBasic.ApplicationServices[^], not an MVC one. However it is indeed executed like this, User.IsInRole(string)[^].

You can consider using the Authorize attribute and pass the role to it,

[Authorize(Roles="Admin")]
public ActionResult YourAction() {
   // Action logic
}





这将为您提供帮助,并且只允许管理员访问内容。请注意,它不适用于内容块,而是应用于操作本身。在第二个想法,您可以考虑请求框架开发人员在应用程序中使用此功能的指导。他们可以解决这个问题,为什么这个函数会抛出异常。



否则,你也可以使用Roles对象并获得用户的结果,





This would do the trick for you and would allow admins only to access the content. Note that it is not applied to a content block, but to the action itself. And on a second thought, you may consider asking that framework developers for guidance on using this function in the application. They would have a solution to this problem as why is this function throwing an exception.

Otherwise, you may also use the Roles object and get the result for user,

if(Roles.IsUserInRole("username", "role-name")) {
   // Content here
}





这种方法很简单,因为它可以应用于内容块而不是整个操作。了解有关 Roles.IsUserInRole(string,string)的更多信息 [ ^ ]。


嗨Afzaal!



i在我的项目中试过这个但同时显示不支持的异常:(
Hi Afzaal!

i tried this in my project but as well showing Exception that it not supported:(
if(Roles.IsUserInRole("username", "role-name")) {
   // Content here
}


请检查您是否正在初始化具有角色的身份提供者



Please check if you are initializing Identity provider with Role

services.AddIdentity<IdentityUser,IdentityRole>()
            .AddEntityFrameworkStores<WebUserContext>()
            .AddDefaultTokenProviders();





请注意,AddDefaultIdentity仅接受IdentityUser,因此不会填充角色。





Please note that AddDefaultIdentity accepts only IdentityUser and hence doesn't populate the Roles.

services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<WebUserContext>()
            .AddDefaultTokenProviders();


这篇关于为什么@ User.IsInRole不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆