小编Kah*_*azi的帖子

ASP.NET核心筛选器-忽略定义的方法

我们将日志信息实现到我们的数据库中。我将为其使用过滤器(IActionFilter)功能。我写了下面的课:

public class ActionFilter: Attribute, IActionFilter
{
    DateTime start;
    public void OnActionExecuting(ActionExecutingContext context)
    {
        start = DateTime.Now;
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        DateTime end = DateTime.Now;
        double processTime = end.Subtract(start).TotalMilliseconds;
        ... some log actions
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我将以下代码添加到Startup.cs中:

services.AddMvc(options => {
            options.Filters.Add(typeof(ActionFilter));

        });
Run Code Online (Sandbox Code Playgroud)

工作正常。我在每个方法的ActionFilter中都有一个断点。

但是我想忽略大部分方法的日志记录。据我了解,我可以使用自己的属性来实现。我以前没有使用自己的属性。好的,我写了以下属性:

public class IgnoreAttribute : Attribute
{
    public IgnoreAttribute()
    { }
}
Run Code Online (Sandbox Code Playgroud)

我将属性添加到方法:

[Ignore]
    [HttpGet]
    [Route("api/AppovedTransactionAmountByDays/{daysCount}")]
    public JsonResult GetAppovedTransactionAmountByDays(int daysCount)
    {
        var result = daysCount;

        return new JsonResult(result);
    }
Run Code Online (Sandbox Code Playgroud)

当然,简单的操作是行不通的。

我如何更改我的属性或ActionFilter以忽略方法?

提前致谢。

c# asp.net custom-attributes asp.net-core-mvc asp.net-core

3
推荐指数
1
解决办法
1801
查看次数

Owin 身份验证不发出 cookie

我在登录控制器中有以下操作。出于测试目的,我没有在 Index 操作中使用登录表单。相反,我创建了声明身份并登录。此操作是 GET 而不是 POST。它创建一个声明标识并将其用于AuthenticationManager.SignIn. 但是当我检查浏览器 cookie 时,我找不到存在的身份验证 cookie。我想弄清楚出了什么问题。

    [AllowAnonymous]
    public ActionResult Index()
    {
        var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "30"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "JFid"));
        identity.AddClaim(new Claim(ClaimTypes.Email, "test"));

        AuthenticationManager.SignIn(new AuthenticationProperties()
        {
            IsPersistent = true,
            ExpiresUtc = DateTime.UtcNow.AddDays(7)

        }, identity);

        return View();
    }
Run Code Online (Sandbox Code Playgroud)

而且我还在 OWIN 中启用了 cookie 身份验证。

[assembly: OwinStartup(typeof(D.Support.WebStartup))]
namespace D.Support
{
    public class WebStartup
    {
        public void Configuration(IAppBuilder app)
        {

        app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
        {
            LoginPath = new PathString("/MyLoginPath"),
            CookieName = "MyCookieName",
            CookieHttpOnly = true,

        });
        }
    } …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc owin asp.net-identity owin-middleware

3
推荐指数
1
解决办法
2113
查看次数

c# .Net Core 2.1 授权属性 - 默认声明

使用 [Authorize] 属性(不指定策略)时,有没有办法在默认策略上设置必需的声明?

关键是我希望它要求授权,但我不想为控制器明确设置策略。我宁愿只有[授权]而不是[授权(政策=“某事”)]

另一种提问方式是,是否可以向默认策略添加声明?

在此先感谢您的任何想法和意见。

c# authorize-attribute asp.net-core asp.net-core-2.0

3
推荐指数
1
解决办法
1030
查看次数

ASP.Net Core 3.0 SignalR HubConnection.InvokeAsync 抛出:'whatever' 的 JSON 属性名称与另一个属性发生冲突

我在我的应用程序中使用了以下 DTO/POCO 类,它通过 SignalR 连接进行通信:

class Base {
     public string Value { get; set; }
}
class Child : Base {
     public new string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这在 ASP.Net Core 2.2 中运行良好。但是,在迁移到 3.0 后,我遇到了以下异常:

 Error Message:
   System.InvalidOperationException : The JSON property name for 'Biz4x.Frontend.Web.DTO.BoardRates.BoardTemplateWithRatesDTO.Template' collides with another property.
  Stack Trace:
     at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(JsonClassInfo jsonClassInfo, JsonPropertyInfo jsonPropertyInfo)
   at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
   at System.Text.Json.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, Object value, Type type, …
Run Code Online (Sandbox Code Playgroud)

c# json.net signalr asp.net-core

2
推荐指数
1
解决办法
1362
查看次数

将 @ref 添加到循环中的 Blazor 组件

如何@ref在循环内添加到 Blazor 组件?

@for (int i = 0; i < 10; i++)
{
    <MyComponent @ref="???"/> // I want to add this component to the list
}

@code
{
    List<MyComponent> components = new List<MyComponent>();
}
Run Code Online (Sandbox Code Playgroud)

c# blazor blazor-compiler

2
推荐指数
2
解决办法
4510
查看次数

在 asp.net mvc 中每 5 分钟刷新一次

控制器显示 Excel 工作表中的数据。我需要控制器每 1 小时检查一次 excel 表,视图也应该更新。这是我的控制器代码:

string path3 = "D:/Project/Sesame Incident Dump_20160317.xls";

Excel.Application application3 = new Excel.Application();
Excel.Workbook workbook3 = application3.Workbooks.Open(path3);
Excel.Worksheet worksheet3 = workbook3.ActiveSheet;
Excel.Range range3 = worksheet3.UsedRange;
List<SesameIncident> ListSesameIncident = new List<SesameIncident>();
for (int row = 2; row <= range3.Rows.Count; row++)
{
    SesameIncident S = new SesameIncident();
    S.Number = (((Excel.Range)range3.Cells[row, 1]).Text);
    S.AssignedTo = (((Excel.Range)range3.Cells[row, 5]).Text);
    S.Opened = (((Excel.Range)range3.Cells[row, 6]).Text);
    S.Status = (((Excel.Range)range3.Cells[row, 7]).Text);
    S.Priority = (((Excel.Range)range3.Cells[row, 10]).Text);
    S.AssignedGroup = (((Excel.Range)range3.Cells[row, 12]).Text);

    ListSesameIncident.Add(S);

}

ViewBag.ListSesameIncidents = ListSesameIncident
       .Where(x …
Run Code Online (Sandbox Code Playgroud)

html asp.net-mvc

1
推荐指数
1
解决办法
4713
查看次数

动态添加包含表达式

我正在使用Entity Framework Core,我有以下内容:

String expression = "Country;User;User.Country"
Run Code Online (Sandbox Code Playgroud)

这表示在查询中包含Country,User和User.Country:

var q = context.Jobs
          .Include(x => x.Country)
          .Include(x => x.User).ThenInclude(x => x.Country);
Run Code Online (Sandbox Code Playgroud)

我不知道将包含什么表达式.我只知道它将是一个实体列表,有或没有子实体(例如:User.Country),我需要构建包含表达式.

有没有办法做到这一点?

c# linq entity-framework entity-framework-core

0
推荐指数
1
解决办法
1366
查看次数