.NET exceptions that I can use for Not Authorized or Not Authenticated

I have parts of the code where I want to throw an exception when the user is not authenticated / not authorized.

So instead of writing my own NotAuthenticatedException and NotAuthorizedException, I was wondering if some C # standards exist for them.

I can imagine that many programs throw such Exceptions, and it would not be very useful if everyone would record their wheel again.

+48
authentication c # exception authorization
Mar 19 '13 at 9:47
source share
3 answers

You can also use UnauthorizedAccessException for authorization violations

+32
Sep 19 '13 at 15:16
source share

To avoid wheel reuse, I would use PrincipalPermission.Demand or PrincipalPermissionAttribute .

A SecurityException will be thrown for you if the request fails.

If you want to explicitly throw an exception rather than using PrincipalPermission.Demand , you can consider reusing the existing type System.UnauthorizedAccessException , which is described in MSDN as

An exception that is thrown when the operating system denies access due to an I / O error or a certain type of security error.

This is your application, not an OS that denies access, but maybe close enough.

+9
Mar 19 '13 at 10:35
source share



All Articles