1

At first I'm beginner in asp. Start by asp.net tutorial to write a web api. after the tutorial finished (I did not add any other code) when I try to test the Api it return me unautherize 401.

I found the below line in my Web.config

<authentication mode="None" />

and in WebApiCOnfig.cs

config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

how can I access to my API without authentication? it's a simple test and no need to any authentication

3
  • Either remove global/controller/action auth filters/attribute or use [AllowAnonymous] attribute on intended controller or action Commented Jun 20, 2017 at 5:24
  • I comment the config.Filters @Nkosi. where should I add [AllowAnonymous]? Commented Jun 20, 2017 at 5:36
  • 1
    No it is one or the other. anyway. if using attribute you can place it either on the controller to apply to all actions in controller, or directly in the action itself for just that action. Commented Jun 20, 2017 at 5:38

1 Answer 1

1

use [AllowAnonymous] attribute on intended controller for all actions on that cotroller

[AllowAnonymous] // Applies to all ations
public class MyController : ApiController {
    public IHttpActionResult MyAction1() {
        //...
    }

    public IHttpActionResult MyAction2() {
        //...
    }
}

or only on the intended action

public class MyController : ApiController {
    [AllowAnonymous] // Applies to only this ation
    public IHttpActionResult MyAction() {
        //...
    }

    //Still secure
    public IHttpActionResult MyAction2() {
        //...
    }
}

When done testing you can remove them.

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

Comments

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.