2

Please provide guidance on how to set users which may operate in application with Windows Authentication on ASP.NET Core

In Asp.Net MVC 5 I do it with web.config:

<authorization>
      <allow users="domain\foo, domain\bar, domain\baz/>
      <deny users="*" />
</authorization>

How implement this with asp.net core?

1 Answer 1

1

You can simply write a global authorization policy:

services.AddMvc(config =>
{
    var policy = new AuthorizationPolicyBuilder()
                   .RequireAssertion(x =>
                         x.User.Identity.Name == "domain\\foo" ||
                         x.User.Identity.Name == "domain\\bar" ||
                         x.User.Identity.Name == "domain\\baz")
                   .Build();
   config.Filters.Add(new AuthorizeFilter(policy));
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! But it hardcoded. Is it possible to make it with project.json or web.config?
My example is just for illustration, you could store user names in appsettings.json or another store. Then you can check if user store contains current user name. Take a look at Configuration docs learn.microsoft.com/en-us/aspnet/core/fundamentals/…
I'm getting the following error even with "services.AddAuthentication(IISDefaults.AuthenticationScheme);" Could you help me with solve this?: InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).

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.