You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Okta SSO与.NET Core Bot Framework集成方法及参考示例咨询

Okta可作为SSO身份提供商与.NET Core Bot Framework v4版本稳定集成,以下是可直接复用的实现流程与代码示例:

前置依赖确认
  • .NET Core 3.1+/NET 6+ 运行环境
  • 已激活的Okta租户账号
  • Bot Framework v4 SDK 核心依赖包
核心实现步骤

第一步:Okta端配置

  • 登录Okta管理后台,创建新的OIDC Web应用,授权类型勾选authorization_code和refresh_token
  • 配置重定向URI为你的Bot的消息端点+/signin-oidc,示例格式为https://你的Bot服务域名/api/messages/signin-oidc
  • 记录下Okta应用的ClientId、ClientSecret、Okta租户域(格式为https://你的租户ID.okta.com)

第二步:.NET Core Bot项目配置

  • 先安装必要的NuGet包:Microsoft.Bot.Builder.Integration.AspNet.Core、Microsoft.AspNetCore.Authentication.OpenIdConnect、Okta.AspNetCore
  • 在appsettings.json中新增对应配置:
"Okta": {
  "OktaDomain": "替换为你的Okta租户域",
  "ClientId": "替换为你的Okta应用ClientId",
  "ClientSecret": "替换为你的Okta应用ClientSecret",
  "AuthorizationServerId": "default",
  "CallbackPath": "/signin-oidc",
  "ResponseType": "code"
},
"Bot": {
  "MicrosoftAppId": "替换为你的Azure Bot应用ID",
  "MicrosoftAppPassword": "替换为你的Azure Bot应用密码"
}
  • 在项目启动文件(.NET 6+为Program.cs,低版本为Startup.cs)中注入认证服务与Bot服务:
// 添加Okta OIDC认证服务
builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOktaMvc(new OktaMvcOptions
{
    OktaDomain = builder.Configuration["Okta:OktaDomain"],
    ClientId = builder.Configuration["Okta:ClientId"],
    ClientSecret = builder.Configuration["Okta:ClientSecret"],
    AuthorizationServerId = builder.Configuration["Okta:AuthorizationServerId"],
    CallbackPath = builder.Configuration["Okta:CallbackPath"],
    ResponseType = OpenIdConnectResponseType.Code,
    GetClaimsFromUserInfoEndpoint = true,
    Scope = new List<string> { "openid", "profile", "email" }
});

// 注入Bot框架核心服务
builder.Services.AddBot<你的自定义Bot类>(options =>
{
    options.CredentialProvider = new SimpleCredentialProvider(
        builder.Configuration["Bot:MicrosoftAppId"],
        builder.Configuration["Bot:MicrosoftAppPassword"]);
});

第三步:Bot对话逻辑中触发SSO认证

在需要身份校验的对话节点中添加认证逻辑,示例代码如下:

private async Task<DialogTurnResult> PromptForLoginAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    if (!stepContext.Context.TurnState.TryGetValue("ClaimsPrincipal", out var claimsPrincipal) || !(claimsPrincipal as ClaimsPrincipal).Identity.IsAuthenticated)
    {
        // 发起Okta认证挑战
        var properties = new AuthenticationProperties { RedirectUri = stepContext.Context.Activity.ServiceUrl };
        await stepContext.Context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, properties, cancellationToken);
        return Dialog.EndOfTurn;
    }
    // 认证通过后读取用户信息执行业务逻辑
    var userEmail = (claimsPrincipal as ClaimsPrincipal).FindFirst(ClaimTypes.Email)?.Value;
    await stepContext.Context.SendActivityAsync($"认证成功,欢迎你 {userEmail}", cancellationToken: cancellationToken);
    return await stepContext.NextAsync(cancellationToken: cancellationToken);
}
常见问题排查
  • 若认证后跳转失败,优先校验Okta应用中配置的重定向URI和实际Bot的回调地址完全一致,包含协议、域名、路径、大小写
  • 调试阶段可查看Okta管理后台的系统日志,定位认证请求的具体报错原因
  • 测试阶段Bot服务需要公网可访问,本地localhost地址无法完成Okta回调校验

内容的提问来源于stack exchange,提问作者JPiasente

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:09:03