HttpListenerException On Using Microsoft Login

huangapple go评论63阅读模式
英文:

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.
HttpListenerException On Using Microsoft Login

答案1

得分: 1

Iam能够使用MSAuth登录托管在Azure应用服务中的Web应用程序。

请检查以下步骤:

  1. AAD中注册应用程序。

HttpListenerException On Using Microsoft Login

  1. 可以从注册的应用程序中下载相同的示例代码。
  2. 转到AAD => 应用注册 => 您的注册应用 => 快速入门 => 单击Web应用程序

HttpListenerException On Using Microsoft Login

  1. 选择ASP.NET Core,您将看到以下屏幕。

  2. 单击为我管理此更改 => 进行更新 => 下载代码示例

HttpListenerException On Using Microsoft Login

我的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

HttpListenerException On Using Microsoft Login

输出:

HttpListenerException On Using Microsoft Login

HttpListenerException On Using Microsoft Login

英文:

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.

HttpListenerException On Using Microsoft Login

  • We can download the sample code for same from the Registered App.
  • Navigate to the AAD => App registrations => Your registered App => Quickstart => click on the Web application

HttpListenerException On Using Microsoft Login

  • Select ASP.NET Core, you will see the below screen.

  • Click on the Manage this change for me => Make updates => Download the code sample.

HttpListenerException On Using Microsoft Login

My appsettings.json :

{
  &quot;AzureAd&quot;: {
    &quot;Instance&quot;: &quot;https://login.microsoftonline.com/&quot;,
    &quot;Domain&quot;: &quot;****.onmicrosoft.com&quot;,
    &quot;ClientId&quot;: &quot;**************&quot;,
    &quot;TenantId&quot;: &quot;********&quot;,
    &quot;CallbackPath&quot;: &quot;/signin-oidc&quot;
  },
  &quot;Logging&quot;: {
    &quot;LogLevel&quot;: {
      &quot;Default&quot;: &quot;Information&quot;,
      &quot;Microsoft.AspNetCore&quot;: &quot;Warning&quot;
    }
  },
  &quot;AllowedHosts&quot;: &quot;*&quot;
}

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(&quot;AzureAd&quot;));
builder.Services.AddControllersWithViews(options =&gt;
{
    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(&quot;/Error&quot;);
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
//app.UseEndpoints(endpoints =&gt;
//{
//    endpoints.MapControllerRoute(
//        name: &quot;default&quot;,
//        pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
//    endpoints.MapRazorPages();
//});
app.MapRazorPages();
app.Run();

My .csproj :

&lt;Project Sdk=&quot;Microsoft.NET.Sdk.Web&quot;&gt;
  &lt;PropertyGroup&gt;
    &lt;TargetFramework&gt;net6.0&lt;/TargetFramework&gt;
    &lt;Nullable&gt;enable&lt;/Nullable&gt;
    &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
  &lt;/PropertyGroup&gt;
  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;Microsoft.Identity.Web&quot; Version=&quot;1.4.0&quot; /&gt;
    &lt;PackageReference Include=&quot;Microsoft.Identity.Web.UI&quot; Version=&quot;1.4.0&quot; /&gt;
  &lt;/ItemGroup&gt;
&lt;/Project&gt;

My Redirect URI:

HttpListenerException On Using Microsoft Login

Output:
HttpListenerException On Using Microsoft Login

HttpListenerException On Using Microsoft Login

huangapple
  • 本文由 发表于 2023年3月9日 18:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定