Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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!
Posted
Updated 14-Sep-18 19:51pm

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,

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


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,

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


This approach is easy because it can be applied to a content block rather than entire action. Read more about Roles.IsUserInRole(string, string)[^] on MSDN.
 
Share this answer
 
v3
Hi Afzaal!

i tried this in my project but as well showing Exception that it not supported:(
C#
if(Roles.IsUserInRole("username", "role-name")) {
   // Content here
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 13-Aug-15 11:14am    
Then consider using the attribute in your application. If that still occurs then you would have to consider writing that to the library developer. They would let you know why are you getting the exception.
Please check if you are initializing Identity provider with Role

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


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

C#
services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<WebUserContext>()
            .AddDefaultTokenProviders();
 
Share this answer
 
Comments
Dave Kreskowiak 15-Sep-18 12:24pm    
I seriously doubt the OP is still struggling with this, THREE YEARS later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900