小编pau*_*sim的帖子

实体框架 - CreateQuery VS ExecuteFunction VS ExecuteStoreQuery VS ExecuteStoreCommand

以下有何不同之处

CreateQuery()  ExecuteFunction(), ExecuteStoreQuery() and ExecuteStoreCommand() 
Run Code Online (Sandbox Code Playgroud)

据我所知,CreateQuery用于Entity SQL,其余的方法用于在DB中定义的sql函数或存储过程.

根据ObjectContext类元数据,它们如下:

CreateQuery():Creates an System.Data.Objects.ObjectQuery<T> in the current object context        by using the specified query string. 
Returned -> System.Data.Objects.ObjectQuery<T>


ExecuteFunction(): Executes a stored procedure or function that is defined in the data source and expressed in the conceptual model; discards any results returned from
the function; and returns the number of rows affected by the execution.
Returned -> The number of rows affected.
This has an overloaded version which return -> The entity …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework executestorequery difference

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

Web Api OWIN - 如何在每个请求上验证令牌

我有两个申请

  1. 客户端应用程序构建在ASP.NET MVC上
  2. 身份验证服务器构建在Web API + OWIN上

计划身份验证如下

  1. 对于用户登录,客户端应用程序将向具有登录凭据的身份验证服务器发出请求.
  2. Authication服务器将生成令牌并将发送回客户端应用程序.
  3. 客户端应用程序将该令牌存储在本地存储中.
  4. 对于每个后续请求,客户端应用程序将附加的令牌保存在请求标头中的本地存

现在,在关闭应用程序的服务器端,我需要确认每个请求的令牌都没有被篡改.

  1. 请建议我如何在每个请求中验证令牌,因为我不知道OWIN用于生成令牌的密钥.
  2. 编写代码以在客户端应用程序上验证令牌是正确的,或者它应该在身份验证服务器上.
  3. 我打算转移所有用户管理代码,如注册用户,将密码更改为认证服务器,以便我们可以将其重新用于不同的客户端应用程序 - 这是正确的设计实践吗?

到目前为止,我已经编写了下面的代码来创建一个POC.

========================= OWIN配置========

    [assembly: OwinStartup(typeof(WebApi.App_Start.Startup))]
    namespace WebApi.App_Start
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                HttpConfiguration config = new HttpConfiguration();

                ConfigureOAuth(app);

                WebApiConfig.Register(config);
                app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
                app.UseWebApi(config);
            }

            public void ConfigureOAuth(IAppBuilder app)
            {
                OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
                {
                    AllowInsecureHttp = false,
                    TokenEndpointPath = new PathString("/token"),
                    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                    Provider = new SimpleAuthorizationServerProvider(),

         };

         // Token Generation

                app.UseOAuthAuthorizationServer(OAuthServerOptions);
                app.UseOAuthBearerAuthentication(new 
 OAuthBearerAuthenticationOptions());

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

c# asp.net-web-api owin http-token-authentication

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

为什么要将基类分配给派生类需要显式转换?但不要求做相反的事情

我有一个基类和一个派生类,如下所示

public class animal
{
    public string name { get; set; }
}

public class dog : animal
{
    public int age { get; set; }
    public string type { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

用途:

animal a = new animal();

dog d = new dog();

a = d; //compiled

d = a; //Error:Cannot implicitly convert type 'animal' to 'dog'.

d = (dog)a; // compiled
Run Code Online (Sandbox Code Playgroud)

内部派生类可以分配给base但是需要进行反向显式转换?即使base和derived类都包含相同的成员,也会发现相同的结果.

c# types derived base variable-assignment

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

Asp.net MVC - 如何检查Ajax请求的会话是否过期

我们正在整个应用程序中使用Ajax调用 - 尝试在尝试执行任何Ajax请求时,如果会话已经过期,则尝试找到重定向到登录页面的全局解决方案.我编写了以下解决方案,从这篇文章中获取帮助 - 在ajax调用中处理会话超时

不确定为什么在我关心的事件中"HandleUnauthorizedRequest"没有被解雇.

自定义属性:

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class CheckSessionExpireAttribute :AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                var url = new UrlHelper(filterContext.RequestContext);
                var loginUrl = url.Content("/Default.aspx");

                filterContext.HttpContext.Session.RemoveAll();
                filterContext.HttpContext.Response.StatusCode = 403;
                filterContext.HttpContext.Response.Redirect(loginUrl, false);
                filterContext.Result = new EmptyResult();
            }
            else
            {
                base.HandleUnauthorizedRequest(filterContext);
            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

在控制器操作中使用Above自定义属性如下:

 [NoCache]
 [CheckSessionExpire]
 public ActionResult GetSomething()
 {
  }
Run Code Online (Sandbox Code Playgroud)

AJAX Call(JS部分):

function GetSomething()
{
   $.ajax({
        cache: false,
        type: "GET",
        async: true,
        url: "/Customer/GetSomething",
        success: function (data) {

        },
        error: function (xhr, …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc custom-attributes expired-sessions ajax-request

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

npgsql.EntityFrameworkCore.PostgreSQL - 使用 json 参数执行函数不起作用

数据库:Postgresql

ORM:带有 Npgsql.EntityFrameworkCore.PostgreSQL 的实体框架核心

尝试调用以下函数将产品列表作为 json 传递

dbContext.product.FromSqlRaw("SELECT pa.usp_set_product({0})", productjson).ToList();
Run Code Online (Sandbox Code Playgroud)

返回错误为:“42883:函数 pa.usp_set_product(text) 不存在”

然后尝试下面

dbContext.product.FromSqlRaw(@"SELECT pa.usp_set_product('" + productjson+ "')").ToList();
Run Code Online (Sandbox Code Playgroud)

返回错误:“输入字符串的格式不正确”

然后尝试下面效果很好

 using (var cmd = new NpgsqlCommand(@"SELECT pa.usp_set_product(@productjson)", conn))
   {
    cmd.Parameters.Add(new NpgsqlParameter("productjson", NpgsqlDbType.Json) { Value = productjson});
    cmd.ExecuteNonQuery();
   }
Run Code Online (Sandbox Code Playgroud)

任何想法,请-

  1. 为什么带有 JSON 参数的 FromSqlRaw 不起作用
  2. 使用 NpgsqlCommand 是否有任何缺点 - 它是否支持 Linux 容器(docker)

谢谢,

@保罗

c# npgsql docker entity-framework-core linux-containers

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