Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在传统路由端点ASP.Net Core中允许匿名_C#_Asp.net Core_Oauth - Fatal编程技术网

C# 在传统路由端点ASP.Net Core中允许匿名

C# 在传统路由端点ASP.Net Core中允许匿名,c#,asp.net-core,oauth,C#,Asp.net Core,Oauth,我有一个允许匿名的路由,配置如下: app.UseMvc(routes => { routes.MapRoute( name: "myEndpoint", template: "myendpoint", defaults: new { controller = "MyEndpoint", action = nameof(MyEndpoint.Ge

我有一个允许匿名的路由,配置如下:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
               name: "myEndpoint",
               template: "myendpoint",
               defaults: new { controller = "MyEndpoint", action = nameof(MyEndpoint.Get) }
            );
        });
问题是它将我重定向到oAuth页面,我希望
myendpoint
允许匿名用户绕过oAuth页面。我尝试在方法和类中使用
AllowAnonymous
属性,但没有成功

这是我的控制器:

namespace MyNamespace
{
   /// <summary>
   /// My Endpoint API
   /// </summary>
   public class MyEndpointController : Controller
   {
       public MyEndpointController()
       {
       }

       /// <summary>
       /// My Endpoint
       /// </summary>
       public MyInfo Get()
       {
           return new Data
           {
              Data = Value
           };
       }
   }
}
名称空间MyNamespace
{
/// 
///我的端点API
/// 
公共类MyEndpointController:控制器
{
公共MyEndpointController()
{
}
/// 
///我的终点
/// 
公共MyInfo Get()
{
返回新数据
{
数据=值
};
}
}
}

将[AllowAnonymous]添加到home controller中,以便匿名用户在注册之前可以获得有关站点的信息

使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Authorization;
名称空间ContactManager.Controllers{
[异名]
公共类HomeController:控制器
{
公共IActionResult索引()
{
返回视图();
}
}

Hi@awais感谢您的回答。我尝试按照您的建议在方法级别和类级别添加属性,但没有效果。它只在我使用属性路由时有效,但我想使用常规路由。您的回答@awais是正确的,但我的启动类有问题。我如何解决它,请参见问题下方的注释tion.hello@MauriR我的启动类是这样的,我也定义了我的默认控制器。它可以与[AllowAnonymous]配合使用。您可以尝试一下。public void Configure(IApplicationBuilder应用程序,IHostingEnvironment环境){app.UseMvc(routes=>{routes.MapRoute(名称:“默认”,模板:{controller=Account}/{action=Login}/{id?}”);}请添加您应用的控制器的代码
AllowAnonymous
attributeHi@Alexander我添加了控制器。谢谢。我会试试看,控制器本身不包含
AuthorizeAttribute
。您配置了全局身份验证吗?也许您也可以共享该代码?我想它有全局身份验证,我是新手但是它在服务中调用了
AddAuthorization
,并且它配置了OAuth。启动类非常大,但是我将尝试剪切不必要的部分并发布它。