1

I'm currently working on a ASP.NET MVC4 website. And in that website i wan't users that are part of an certain role to be allowed to run the code. I use the following code:

    [Authorize(Roles = GROUP)]
    public void Auth()
    {
        /* if user is in the domain and signed in
         * and a member of the above group 
         * they will come here */

        username = User.Identity.Name;

        //Do somthing
    }

And this works great, but when the user isn't part of the domain and/or group it wil prompt for username and password. Is it possible to skip the prompt and just redirect that user?

This website is setup in a IIS 8 with authentication set to windows authentication

2
  • r u using windows authentication? Commented Mar 27, 2014 at 11:14
  • Yes, in a IIS 8 with authentication set to windows authentication Commented Mar 27, 2014 at 11:17

2 Answers 2

3

Well I would create a Custom Authorization Attribute and implement HandleUnauthorizedRequest method to solve this problem.

public class CustomAutorizeAttribute : AuthorizeAttribute
{
   protected override bool AuthorizeCore(HttpContextBase httpContext)
   {
      // do authorization logic
      // ...


      return (/* isAuthorized */);
   }


   protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
   {
      UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);


      filterContext.Result = new RedirectResult(urlHelper.Action("Index", "Error"));
   }
}

For more information read How to: Create a Custom Authorization Attribute

Sign up to request clarification or add additional context in comments.

1 Comment

this is the correct way of authorization as you wanted to tell the user that you don't have the permission.
1

use

 [Authorize(Roles = GROUP)]
  [HandleError(ExceptionType = typeof(UnauthorizedAccessException), View = "ApplicationError")]
    public void Auth()
    {
        /* if user is in the domain and signed in
         * and a member of the above group 
         * they will come here */

        username = User.Identity.Name;

        //Do somthing
    }

where you can sepcify view for unauthorized access user

1 Comment

Thanks for the suggestion but it still prompts for username and password

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.