英文:
HttpListenerException On Using Microsoft Login
问题
使用MS Auth登录用户时,出现了以下问题,尝试在浏览器中打开MS UI以进行身份验证时发生了HttpListenerException异常。
"在尝试在 http://localhost:49299/ 上监听系统浏览器完成登录时发生了HttpListenerException异常。可能的原因和解决方法:应用程序无法监听指定的URL;请从管理员命令提示符运行 'netsh http add iplisten 127.0.0.1'。访问被拒绝。"
我甚至尝试了授予权限,但仍然遇到了访问被拒绝的错误。
英文:
I am using MS Auth to login the user for my .NET Core 6 web app hosted in Azure app service. Getting following when trying to open MS UI in browser for authentication.
An HttpListenerException occurred while listening on http://localhost:49299/ for the system browser to complete the login. Possible cause and mitigation: the app is unable to listen on the specified URL; run 'netsh http add iplisten 127.0.0.1' from the Admin command prompt. Access is denied.
I even tried proving the access, but getting Access Denied error.
答案1
得分: 1
Iam能够使用MSAuth
登录托管在Azure应用服务中的Web应用程序。
请检查以下步骤:
- 在
AAD
中注册应用程序。
- 可以从
注册的应用程序
中下载相同的示例代码。 - 转到
AAD
=>应用注册
=> 您的注册应用
=>快速入门
=> 单击Web应用程序
。
-
选择
ASP.NET Core
,您将看到以下屏幕。 -
单击
为我管理此更改
=> 进行更新 =>下载代码示例
。
我的appsettings.json
:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "****.onmicrosoft.com",
"ClientId": "**************",
"TenantId": "********",
"CallbackPath": "/signin-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
我的Program.cs
:
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.AspNetCore.Mvc.Authorization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
builder.Services.AddRazorPages()
.AddMicrosoftIdentityUI();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
我的.csproj
:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="1.4.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.4.0" />
</ItemGroup>
</Project>
我的重定向URI
:
输出:
英文:
Iam able to login the user with MSAuth
for the web app hosted in Azure app service.
Check the below steps:
Register the Application in AAD
.
- We can download the sample code for same from the
Registered App
. - Navigate to the
AAD
=>App registrations
=> Yourregistered App
=>Quickstart
=> click on theWeb application
-
Select
ASP.NET Core
, you will see the below screen. -
Click on the
Manage this change for me
=> Make updates =>Download the code sample
.
My appsettings.json
:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "****.onmicrosoft.com",
"ClientId": "**************",
"TenantId": "********",
"CallbackPath": "/signin-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
My Program.cs
:
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.AspNetCore.Mvc.Authorization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
builder.Services.AddRazorPages()
.AddMicrosoftIdentityUI();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/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?}");
// endpoints.MapRazorPages();
//});
app.MapRazorPages();
app.Run();
My .csproj
:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web" Version="1.4.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.4.0" />
</ItemGroup>
</Project>
My Redirect URI
:
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论