ASP.NET Core LDAP Active Directory 认证
创始人
2024-09-15 05:30:29
0

以下是一个使用ASP.NET Core进行LDAP Active Directory身份验证的示例代码:

  1. 首先,您需要在Startup.cs文件中配置LDAP身份验证服务。
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace YourProjectNamespace
{
    public class Startup
    {
        private readonly IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
                {
                    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                })
                .AddCookie()
                .AddOpenIdConnect(options =>
                {
                    options.Authority = "https://your-ldap-server-url";
                    options.ClientId = "your-client-id";
                    options.ResponseType = "code";
                    options.CallbackPath = "/signin-oidc";
                    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.UsePkce = false;
                    options.SaveTokens = true;
                    options.TokenValidationParameters.ValidateIssuer = false;
                    options.TokenValidationParameters.NameClaimType = "preferred_username";
                });

            services.AddControllersWithViews();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // ...

            app.UseAuthentication();
            app.UseAuthorization();

            // ...
        }
    }
}
  1. 接下来,您可以在Controller中使用[Authorize]属性来保护需要认证的操作。
using Microsoft.AspNetCore.Mvc;

namespace YourProjectNamespace.Controllers
{
    public class HomeController : Controller
    {
        [Authorize]
        public IActionResult Index()
        {
            // 这个操作需要认证
            return View();
        }

        // ...
    }
}
  1. 最后,您可以在视图中使用标签来显示用户信息。
@using Microsoft.AspNetCore.Authentication

@if (User.Identity.IsAuthenticated)
{
    

Welcome, @User.Identity.Name!

} else {

Anonymous

Login }

这是一个基本的示例,您可以根据自己的需求进行自定义和扩展。

相关内容

热门资讯

透视辅助“打哈儿麻将辅助软件”... 您好:打哈儿麻将辅助软件这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的...
我来教教大家“闲来辅助神器下载... 我来教教大家“闲来辅助神器下载2022”先前有辅助开挂插件(的确有挂);无需打开直接搜索微信(136...
透视真的“中至窝龙辅助”素来有... 透视真的“中至窝龙辅助”素来有辅助开挂插件(揭秘有挂);无需打开直接搜索微信(136704302)咨...
透视软件“中至余干万能辅助器”... 透视软件“中至余干万能辅助器”先前有辅助开挂脚本(有挂工具) 了解更多开挂安装加(136704302...
透视真的“酷玩联盟破解版下载”... 透视真的“酷玩联盟破解版下载”素来有辅助工具(了解有挂);无需打开直接搜索加(薇:136704302...
一分钟快速了解“兴动海满辅助”... 您好:兴动海满辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
技术分享“赣牌圈控制牌型”本然... 技术分享“赣牌圈控制牌型”本然有开挂辅助器(有挂总结);无需打开直接搜索加薇136704302(咨询...
总算了解“pokemmo免费脚... 总算了解“pokemmo免费脚本”本然有开挂辅助软件(确实有挂);无需打开直接搜索加(薇:13670...
推荐攻略“兴动互娱修改器免费”... 推荐攻略“兴动互娱修改器免费”素来有辅助开挂软件(讲解有挂) 了解更多开挂安装加(136704302...
新手必备“陕西三代辅助”往昔有... 新手必备“陕西三代辅助”往昔有辅助安装(有挂猫腻) 了解更多开挂安装加(136704302)微信号陕...