.net core 第三方Microsoft账号登录
微软提供了sdk,有相关实例代码,可以直接去下载运行看看
1. 配置Microsoft Azure客户端
https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
我这里已经配置好了
配置重定向地址:我这里是测试,就直接用: localhost+本地项目运行的端口
2. 项目代码
添加配置文件
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
// "Domain": "[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]",
"ClientId": "your clientid",
//"ClientSecret": "xxx",
"TenantId": "consumers",
"CallbackPath": "/signin-oidc"
}
相关服务注入
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddRazorPages()
.AddMicrosoftIdentityUI();
//builder.Services.AddIdentityCore();
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
app.UseAuthentication(); app.UseAuthorization();
需要授权的地方打上授权标签
OK,最后运行看看效果
这里只是简单的例子,如果需要进一步了解可以看官方提供的文档