desperate man
desperate man

Reputation: 904

how to throw error about user's role mismatch?

If I set up in web.config such code:

<location path="SomeFolder/SimePage.aspx">
    <system.web>
        <authorization>
            <allow roles="Role1" />
            <allow roles="Role2" />
            <deny users="*" />
        </authorization>
    </system.web>
</location>

And when the user with the wrong role comes to SomeFolder/SimePage.aspx then he sees login page. But how can I throw a custom error that will notify him about his wrong role?

Upvotes: 2

Views: 102

Answers (1)

utsikko
utsikko

Reputation: 1545

First, create a custom error page with a suitable message notifying the user about his/her wrong role. Then, open the code-behind of the page you want to restrict, and eventually display the custom error, select Page Events and the event Load. When in the Protected Sub Page_Load, use the following code:

If Not (User.IsInRole("Role1") Or User.IsInRole("Role2")) Then
    Response.Redirect("Path/To/Folder/CustomError.aspx")
End If

Now, every time user will not be in one of these roles, he/she will get the custom error page.

Hopefully, this is helpful.

Upvotes: 1

Related Questions