services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication1", Version = "v1" }); c.DocInclusionPredicate((name, api) => api.HttpMethod != null);
// Locate the XML file being generated by ASP.NET var xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "WebApplication1.API.xml", SearchOption.TopDirectoryOnly);
// Tell Swagger to use those XML comments. xmlFiles.ToList().ForEach(xmlFile => c.IncludeXmlComments(xmlFile)); });
将应用包上传到teams中测试:Teams 左边栏点击Apps —> Manage your apps —> Upload an app to your org’s app catalog 上传后图标显示在Apps列表中 可将其添加到team的chat中
发布到组织
Todo List Sample (SPFx)
1 2
npm i -g teamsfx-cli // 安装脚手架 teamsfx new template list // 查看可用的sample
使用如下命令创建sample APP并基于这些sample作为模板开发自己的应用
1
teamsfx new template todo-list-SPFx
在teams中打开sharepoint站点 配置一个list 见Microsoft Doc: 代码中/SPFx/src/webparts/todoList/components/SharePointListManager.ts line17 设置为sharepoint上list的名字 用vscode的Teams Toolkit打开项目 选择项目目录打开 在teams toolkit中选择Provision in the cloud 在Cloud端创建app ** 点击Deploy to the cloud部署 点击Publish to the cloud发布 访问Microsoft Teams admin center查找已发布的app 登录Teams添加app TODO 主动从外部系统向 Teams 发送信息,允许用户从 Teams 客户端内部处理该信息。 TODO 允许用户在另一个系统中快速查找信息,并将结果添加到 Teams 中的对话。
身份认证
Microsoft 或 Microsoft 365 帐户登录 Teams 后,应用用户可以使用你的应用,而无需再次登录。 如上图示,App添加到Teams,切换到该tab时 确认授权App访问Azure AD
dotnet new webapi -n Api dotnet sln add .\Api\Api.csproj
受保护的接口(protected interface)
1 2 3 4 5 6 7 8 9 10 11
[Route("identity")] [ApiController] [Authorize] public class IdentityController : ControllerBase { [HttpGet] public IActionResult Get() { return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); } }
class Program { public static async Task Main(string[] args) { var client = new HttpClient(); var disco = await client.GetDiscoveryDocumentAsync("https://localhost:5001"); if (disco.IsError) { Console.WriteLine(disco.Error); return; } var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = disco.TokenEndpoint, ClientId = "client", ClientSecret = "secret", Scope = "api1" });;
if (tokenResponse.IsError) { Console.WriteLine(tokenResponse.Error); return; }
Console.WriteLine(tokenResponse.Json);
// call api var apiClient = new HttpClient(); apiClient.SetBearerToken(tokenResponse.AccessToken); var response = await apiClient.GetAsync("https://localhost:44323/identity"); if (!response.IsSuccessStatusCode) { Console.WriteLine(response.StatusCode); } else { var content = await response.Content.ReadAsStringAsync(); Console.WriteLine(JArray.Parse(content));
Similar to OAuth 2.0, OpenID Connect also uses the scopes concept. Again, scopes represent something you want to protect and that clients want to access. In contrast to OAuth, scopes in OIDC don’t represent APIs, but identity data like user id, name or email address.