| |

VerySource

 Forgot password?
 Register
Search
View: 620|Reply: 3

Determine if a user belongs to a role?

[Copy link]

4

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-22 15:40:01
| Show all posts |Read mode
protected void Page_Load (object sender, EventArgs e)
    {
        RolePrincipal p = (RolePrincipal) User;
        string [] roles = p.GetRoles ();
        foreach (string s in roles)
        {
            Response.Write (s + "<br>");
        }
        if (HttpContext.Current.User.IsInRole ("PurchaseAdmin"))
        {
            string url;
            url = "~ / Default1.aspx";
            Response.Redirect (this.ResolveUrl (url));
                   }
    }

The following error occurred during operation, I seem to have solved it before, but the problem cannot be solved after reinstalling the operating system. ?????
The error is as follows:
Objects of type "System.Security.Principal.WindowsPrincipal" cannot be cast to type "System.Web.Security.RolePrincipal".
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-2-1 20:09:01
| Show all posts
((RolePrincipal) Page.User.Identity) .IsInRole
Reply

Use magic Report

4

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-2-4 17:00:01
| Show all posts
I solved it myself, it turned out that I removed the enable windows authentication and role authentication in WEB.CONFIG, so it reported an error. It was normal after it was enabled.
Reply

Use magic Report

4

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-2-6 11:30:01
| Show all posts
The code is improved (purpose: users belong to different roles and then go to different pages).

protected void Page_Load (object sender, EventArgs e)
    {
        RolePrincipal p = (RolePrincipal) User;
        string [] roles = p.GetRoles ();
        foreach (string s in roles)
        {
            Response.Write (s + "<br>");
            string str1 = "Purchasing system administrator group";
            string str2 = s;
            if (str2.IndexOf (str1)> -1)
            {
                //Response.Write("contains ");
                string url;
                url = "~ / Default1.aspx";
                Response.Redirect (this.ResolveUrl (url));
            }
         }
     }
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list