首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在我的ASP.Net Core2.0项目中实现活动目录?

在ASP.Net Core 2.0项目中实现活动目录,可以通过以下步骤进行:

  1. 首先,确保你的ASP.Net Core 2.0项目已经安装了Microsoft.AspNetCore.Authentication.ActiveDirectory包。可以通过NuGet包管理器或者在.csproj文件中手动添加引用来安装该包。
  2. 在Startup.cs文件中,添加以下代码来配置活动目录认证:
代码语言:csharp
复制
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;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddOpenIdConnect(options =>
        {
            options.Authority = "https://login.microsoftonline.com/{your-tenant-id}";
            options.ClientId = "{your-client-id}";
            options.ClientSecret = "{your-client-secret}";
            options.CallbackPath = "/signin-oidc";
            options.ResponseType = "code";
            options.SaveTokens = true;
            options.Events = new OpenIdConnectEvents
            {
                OnRemoteFailure = OnAuthenticationFailed
            };
        });

        services.AddControllersWithViews();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

    private Task OnAuthenticationFailed(RemoteFailureContext context)
    {
        context.HandleResponse();
        context.Response.Redirect("/Home/Error?message=" + context.Failure.Message);
        return Task.CompletedTask;
    }
}

在上述代码中,需要替换以下参数:

  • {your-tenant-id}: 替换为你的租户ID。
  • {your-client-id}: 替换为你的客户端ID。
  • {your-client-secret}: 替换为你的客户端密钥。
  1. 在需要进行活动目录认证的Controller或Action上,添加[Authorize]属性,以确保只有经过认证的用户可以访问。
代码语言:csharp
复制
[Authorize]
public class HomeController : Controller
{
    // ...
}

这样,当用户访问需要认证的页面时,系统将会重定向到活动目录登录页面进行认证。

请注意,以上代码仅为示例,实际使用时需要根据自己的具体情况进行配置和调整。

推荐的腾讯云相关产品:腾讯云身份认证服务(CAM)

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

.Net Core 认证组件之Cookie认证组件解析源码

接着上文.Net Core 认证系统源码解析,Cookie认证算是常用的认证模式,但是目前主流都是前后端分离,有点鸡肋但是,不考虑移动端的站点或者纯管理后台网站可以使用这种认证方式.注意:基于浏览器且不是前后端分离的架构(页面端具有服务端处理能力).移动端就不要考虑了,太麻烦.支持前后端分离前给移动端提供认证Api的一般采用JwtBearer认证,可以和IdentityServer4的password模式结合.很适用,但是id4的password模式各客户端必须绝对信任,因为要暴露用户名密码.适合做企业级下所有产品的认证.不支持除企业外的第三方调用.当然id4提供了其他模式.这是题外话.但是场景得介绍清楚.以免误导大家!

01
领券