How to get windows user name when enabling both windows authentication and anonymous authentication

杀马特。学长 韩版系。学妹 提交于 2019-12-05 18:27:37
  • Deploy your application to IIS and then Open the Authentication menu for the site.

  • Disable Anonymous and enable Windows Authentication

  • Add the following to the ConfigureServices method:

    //using Microsoft.AspNetCore.Server.IISIntegration;

    services.AddAuthentication(IISDefaults.AuthenticationScheme)

  • For the APIs or action controllers that you want to secure, decorate them with [Authorize] attribute, then you get the logged in user using HttpContext.User.Identity.Name. Use [AllowAnonymous] on actions that you want to allow access.

In case you want to secure and allow access on the same api, then you need to provide your own implementation of the Authorization filter.

For more details check this link

Here's how I solved this:

IIS

Since you want to allow anonymous users to hit some endpoints of your API, you need to enable both anonymous authentication and Windows authentication.

As a side note, you're right saying that [AllowAnonymous] has no effect when only Windows authentication is enabled because IIS, which sits in front of your API, will reject anonymous requests.

ASP.NET Core authentication

Now that anonymous authentication is enabled, IIS will not try to authenticate requests by default, so without any further configuration, all requests will be anonymous as far as ASP.NET Core is concerned.

The answer to this is to indicate to ASP.NET Core that you want to try to run the Windows authentication process on every request. You can do this this way:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Other code omitted for brievity

        // This sets the IIS authentication scheme as the default scheme
        services.AddAuthentication(IISDefaults.AuthenticationScheme);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // Other code omitted for brievity

        // This includes the authentication middleware in the request pipeline
        // It will try to authenticate every incoming request
        app.UseAuthentication();

        // MVC comes next, so the authentication will have taken place
        // by the time your controller action is executed against the scheme
        // used in AddAuthentication
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

MVC

There's now 2 possibilities in your controller:

  • The client is compatible with Windows authentication, so User.Identity.IsAuthenticated will return true
  • The client is not compatible with Windows authentication, so User.Identity.IsAuthenticated will fetch the value false

This means that you can either use the [Authorize] attribute on the specific actions that require authentication, or add the AuthorizeAttribute globally to the application and use [AllowAnonymous] on the actions that can be called anonymously.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!