小编Hen*_*man的帖子

如果我想使用ASP.NET MVC4创建ApiKey受限资源,我应该使用IAuthorizationFilter吗?

我有一些简单的路由,我希望通过一个简单的查询字符串参数进行限制.如果密钥不正确或未提供,那么我希望抛出一个NotAuthorizedException.

请不要建议我使用WebApi或等效 - 我不能在这种情况下使用.

所以我不确定我是应该实现IAuthorizationFilter或实现一个IActionFilter甚至是别的东西.

我的代码逻辑?

  • 检查查询字符串是否有关键.
  • 检查我的RavenDb(存储库)以查找具有该键/值的用户.

如果他们没有通过任何这些检查,那么扔掉NotAuthorizedException.

我假设我会用这个过滤器装饰我的动作方法.我还假设我需要将我的存储库传递给这个动作方法吗?

有什么建议吗?

.net c# authorization asp.net-mvc-4

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

C#BackgroundWorker的文化

我想为我的整个应用程序设置文化.我尝试了以下方法:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(wantedCulture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(wantedCulture);
Application.CurrentCulture = CultureInfo.CreateSpecificCulture(wantedCulture);
Run Code Online (Sandbox Code Playgroud)

它适用于当前线程,但稍后我创建并启动后台工作线程.当我创建worker时,当前线程使用wantedCulture执行,但工作线程将使用我的计算机文化运行.

为整个应用程序设置文化的任何想法?

c# culture multithreading

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

正则表达式查找以特定字符开头的单词

我试图找到一个特定字符开头的单词,如:

Lorem ipsum #text Second lorem ipsum.你好吗.没关系.完成.现在#else.

我需要用"#"开头所有单词.所以我的预期结果是#text,#are,#else

有任何想法吗?

c# regex

19
推荐指数
2
解决办法
3万
查看次数

解决'构造函数中的虚方法调用'问题

我在c#中制作软件.我正在使用一个抽象类,Instruction它有这些代码:

protected Instruction(InstructionSet instructionSet, ExpressionElement newArgument,
    bool newDoesUseArgument, int newDefaultArgument, int newCostInBytes, bool newDoesUseRealInstruction) {

    //Some stuff

    if (DoesUseRealInstruction) {
        //The warning appears here.
        RealInstruction = GetRealInstruction(instructionSet, Argument);
    }
}
Run Code Online (Sandbox Code Playgroud)

public virtual Instruction GetRealInstruction(InstructionSet instructionSet, ExpressionElement argument) {
    throw new NotImplementedException("Real instruction not implemented. Instruction type: " + GetType());
}
Run Code Online (Sandbox Code Playgroud)

因此,Resharper告诉我,在标记的行中,我"在构造函数中调用虚方法"并且这很糟糕.我理解构造函数被调用的顺序.该GetRealInstruction方法的所有覆盖内容如下所示:

public override Instruction GetRealInstruction(InstructionSet instructionSet, ExpressionElement argument) {
    return new GoInstruction(instructionSet, argument);
}
Run Code Online (Sandbox Code Playgroud)

所以他们不依赖于班上的任何数据; 他们只返回依赖于派生类型的东西.(因此构造函数顺序不会影响它们).

那么,我应该忽略它吗?我宁愿不; 所以任何人都可以告诉我如何避免这种警告?

我不能巧妙地使用委托,因为该GetRealInstruction方法还有一个重载.

c# resharper inheritance constructor

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

使用Parallel.ForEach得到不同的求和结果

我有一个foreach循环,我正在并行化,我注意到一些奇怪的东西.代码看起来像

double sum = 0.0;

Parallel.ForEach(myCollection, arg =>
{
     sum += ComplicatedFunction(arg);
});

// Use sum variable below
Run Code Online (Sandbox Code Playgroud)

当我使用常规foreach循环时,我会得到不同的结果.内部可能存在更深层次的内容,ComplicatedFunctionsum变量可能会受到并行化的意外影响?

c# parallel-processing task

18
推荐指数
3
解决办法
8025
查看次数

从整数流中找出中位数

给定一个未排序的整数序列,它作为流流入您的程序.

整数太多,不适合记忆.

想象一下有一个功能:

int getNext() throws NoSuchElementException;
Run Code Online (Sandbox Code Playgroud)

它返回流中的下一个整数.

写一个函数来找到中位数.

解决O(n)中的问题.

有任何想法吗?

给出提示(使用堆数据结构..)

algorithm

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

如何在C#和C++中测量经过的时间

我有一个简单的C#和C++代码来计算点积的总和.

C#代码是:

using System;

namespace DotPerfTestCS
{
    class Program
    {
        struct Point3D
        {
            public double X, Y, Z;

            public Point3D(double x, double y, double z)
            {
                X = x;
                Y = y;
                Z = z;
            }
        }

        static void RunTest()
        {
            unchecked
            {
                const int numPoints = 100000;
                const int numIters = 100000000;

                Point3D[] pts = new Point3D[numPoints];
                for (int i = 0; i < numPoints; i++) pts[i] = new Point3D(i, i + 1, i + 2);

                var begin = …
Run Code Online (Sandbox Code Playgroud)

c# c++ performance

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

字符串连接是否可靠?

我如何连接字符串的可靠性?

var str = 'asdf'
var b = str + 'sdf'
Run Code Online (Sandbox Code Playgroud)

似乎不起作用..

我查阅了文档(https://github.com/ethereum/wiki/wiki/Solidity-Tutorial#elementary-types-value-types),并没有提到有关字符串连接的内容.但据说它适用于点('.')?

"[...] a mapping key k is located at sha3(k . p) where . is concatenation."
Run Code Online (Sandbox Code Playgroud)

也没为我工作..:/

ethereum solidity

17
推荐指数
5
解决办法
1万
查看次数

使用System.ServiceModel.ServiceAuthenticationManager进行自定义WCF身份验证?

我正在进行自定义WCF身份验证和授权,并找到一些关于UserNamePasswordValidatorServiceAuthorizationManager的文章.

我还发现了使用自定义System.ServiceModel的线索.ServiceAuthenticationManager(死链接 ),但msdn并没有详细说明它(http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceauthenticationmanager.aspx).

所以我在这里:任何人都更了解ServiceAuthenticationManager?

通常,您将如何设置自定义WCF身份验证?

authentication wcf

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

.net中的Tag属性有什么用处

我注意到带控件的Tag属性.是否可以使用它来引用我的自定义对象,或者我应该远离它,因为它需要装箱和拆箱,这被提到是不安全的并且不推荐.

TreeNode tn = new TreeNode ();
CustClass o = new CustClass()
o.number = 123;
tn.Tag = o;

class CustClass
{
    public int number {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

.net c# .net-2.0 winforms

16
推荐指数
2
解决办法
4万
查看次数