小编max*_*pan的帖子

使用 HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress 时获取 127.0.0.1

我正在使用带有 mvc 的 asp.net 核心。我正在尝试使用以下代码获取 IP 地址。

var ipAddress = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress?.ToString();
Run Code Online (Sandbox Code Playgroud)

它总是返回 ::1 这意味着我本地机器上的 127.0.0.1 这很好。但是现在我将它托管在 azure cloud 上,仅用于使用我的测试 azure 帐户进行测试,它仍然给我 127.0.0.1。

我做错了什么?

项目.json

    {
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "author": [ "Musaab Mushtaq", "Programmer" ],
      "type": "platform"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0-rc2-final",
    "DeveloperForce.Force": "1.3.0",
    "Microsoft.Framework.Configuration": "1.0.0-beta8",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "IntegraPay.Domain": "1.0.0-*"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": …
Run Code Online (Sandbox Code Playgroud)

c# azure asp.net-core-mvc asp.net-core

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

如何在asp.net mvc核心中编写Identity Server 4的集成测试

我正在使用身份服务器4的web api.我不知道从哪里开始编写集成测试.我有一个登录控制器接收用户名和密码,用于ResourceOwnerPassword授权类型.以下是我的代码.

控制器.

[Route("Authentication/Login")]
public async Task<IActionResult> WebApiLogin(string username, string password)
{
    var accessToken = await UserAccessToken.GenerateToken(username, password);
    return new JsonResult(accessToken);
}
Run Code Online (Sandbox Code Playgroud)

用于生成令牌的类

public async Task<string> GenerateToken(string username, string password)
{
    //discover endpoint for metadata
    var disco = await DiscoveryClient.GetAsync("http://localhost:5000");

    //request token
    var clientToken = new TokenClient(disco.TokenEndpoint, "client", "secret");
    //var tokenResponse = await clientToken.RequestClientCredentialsAsync("Payment");
    var tokenResponse = await clientToken.RequestResourceOwnerPasswordAsync(username, password, "IntegrapayAPI");

    if (tokenResponse.IsError)
    {
        //Error tokenResponse.Error
        return tokenResponse.Error;
    }
    return tokenResponse.Json.ToString();
}
Run Code Online (Sandbox Code Playgroud)

IdentityServer Project启动类.

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentityServer() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-core identityserver4

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

使用soql从销售人员获取选择列表记录

我正在尝试使用 soql 从销售人员获取选项列表记录。我的表名称是Industry__c,它有一个名为Industry_Group__c的字段,其数据类型是picklist。如何获取 Industry_group__c 选择列表的记录。

我已经尝试过以下查询,但没有成功。

select Industry_Group__c  from Industry__c
Run Code Online (Sandbox Code Playgroud)

salesforce

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

for循环需要很长时间才能完成

以下代码工作正常.唯一的问题是,如果我给出一个长度为200,000的数组值,则需要太长时间才能完成约1小时.以下是我的代码.

这里有数据文件,其中包含数据和data2字符串值.数据数组按降序排列,data2按升序排列.

public void GetInputs()
{
    string data;
    string data2;
    string[] scores_temp = data.Split(' ');
    int[] scores = Array.ConvertAll(scores_temp, Int32.Parse);

    string[] alice_temp = data2.Split(' ');
    int[] aliceScore = Array.ConvertAll(alice_temp, Int32.Parse);

    var distinctScores = scores.Distinct().ToList();
    int rank = 0;
    for (int j = 0; j <= aliceScore.Length-1; j++)
    {
        for (int i = distinctScores.Count-1; i >= 0; i--)
        {
            if (aliceScore[j] >= distinctScores[i])
            {
                rank = distinctScores.IndexOf(distinctScores[i]);
            }
            else if (aliceScore[j] < distinctScores[i])
            {
                rank = distinctScores.IndexOf(distinctScores[i]); …
Run Code Online (Sandbox Code Playgroud)

c#

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

无法在 asp.net core 2.0 中使用 CookieAuthenticaton 和 openidConnect 身份验证

我已将我的项目升级到 asp.net 核心。但是现在我的 CookieAuthnetication 和 OpenIdConnectionAuthentication 方法不起作用。它们已经过时了。

Startup.cs 配置方法

 app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationScheme = "Cookies"
            });

            app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
            {
                AuthenticationScheme = "oidc",
                SignInScheme = "Cookies",
                Authority = "http://localhost:5000",
                RequireHttpsMetadata = false,
                ClientId = "integapay.client",
                ClientSecret = "mySecret",
                ResponseType = "code id_token",
                Scope = { "openid", "profile", "api.public", "offline_access" },
                GetClaimsFromUserInfoEndpoint = true,
                SaveTokens = true
            });
Run Code Online (Sandbox Code Playgroud)

asp.net-core identityserver4 asp.net-core-mvc-2.0

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

使用正则表达式获取每个反斜杠之间的字符串

我希望正则表达式组中的'\'反斜杠之间的内容.

C:\\Development\\TestEnvironment\\VIdeo\MyVideo.mp3
Run Code Online (Sandbox Code Playgroud)

c# regex

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

字符串引用检查在我的单元Test中返回true

以下是我的测试用例.两个字符串都是不同的对象,但我的测试用例通过.

我期待他们失败.因为它们是不同的对象.

 string string1 = "Hello World";
    string string4 = "Hello World";
    Assert.AreSame(string1, string4);//Will return true
    Assert.IsTrue(object.ReferenceEquals(string1,string4));
Run Code Online (Sandbox Code Playgroud)

c#

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

如何在ASP.NET Core 2.0中使用TLS 1.2

直到我的salesforce res api都运行良好。突然我开始收到身份验证错误。重试您的请求。 Salesforce.Common.AuthenticationClient.d__1.MoveNext()

salesforce通知它将从现在开始使用TLS .1.2。如何在Startup.cs中强制我的asp.net core 2.0使用TLS 1.2。下面是我的登录代码。

 private async Task<AuthenticationClient> GetValidateAuthentication()
        {
            RestApiSetting data = new RestApiSetting(Configuration);
            var auth = new AuthenticationClient();
            var url = data.IsSandBoxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase)
                ? "https://test.salesforce.com/services/oauth2/token"
                : "https://login.salesforce.com/services/oauth2/token";
            try
            {
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                await auth.UsernamePasswordAsync(data.ConsumerKey,
                data.ConsumerSecret, data.Username, data.Password, url);
                return auth;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
Run Code Online (Sandbox Code Playgroud)

c# salesforce asp.net-core-2.0

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

如何将Long []转换为IEnumerable &lt;long?&gt;

我有一个例外

无法将类型'long []'隐式转换为'System.Collections.Generic.IEnumerable <long?>'

我该如何解决这个问题?

我已经尝试过了,但是没有用。

AssignedPlayerSites是IList属性。

IEnumerable<long?> multipleSelectedSites = cmsUser.AssignedPlayerSites.Select(ps => ps.Id).AsEnumerable<long?>();
Run Code Online (Sandbox Code Playgroud)

c#

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

在asp.net Web API中使用FromBody时,字符串值是Empty

我正在使用asp.net核心Web API。下面是我的简单post函数,它具有一个字符串参数。问题是当我使用[FromBody]时,字符串保持为空。我正在使用PostMan来测试我的服务。我希望原始数据从客户端传递到我的控制器。在Postman中,我选择主体类型RAW,并设置标题Content-Type文本/纯文本。Raw Body包含“ Hello World”字符串。

[HttpPost]
        [Route("hosted-services/tokenize-card")]
        public IActionResult Test([FromRoute]decimal businessKey,[FromBody] string body)
        {
            var data = businessKey;
            return new JsonResult("Hello World");
        }
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-web-api

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

在 C# 中获得嵌套 for 循环的性能影响

我有一个字符串数组,总共(100k)。我需要检查之前是否出现过相同的字符串,如果出现,我所要做的就是返回该字符串。我已经使用嵌套 for 循环编写了代码,但不幸的是我的性能很差。为 (string[100K]) 正确处理函数需要 1.9 分钟,而我希望它在几秒钟内完成。

我关心的不是逻辑。我关心的是 LOOP。如何提高循环的效率。

public string StartMatchingProcess(string[]inputArray)
{
    string[] stringArray = inputArray;
    string result = string.Empty;

    for (long i = 0; i < stringArray.Length; i++)
    {
        for (long j = 0; j <= i; j++)
        {
            if(i == j) break;

            if (IsPrefix(stringArray[i], stringArray[j]))
            {
                return stringArray[i];
            }
        }
    }
    Console.WriteLine("GOOD SET");
    return result;
}

public bool IsPrefix(string string1,string string2)
{
    if (AreString1ValuesValid(string1, string2))
    {
        if (string1 == string2.Substring(0, string1.Length))
        {
            Console.WriteLine("BAD SET");
            Console.WriteLine(string1);
            return …
Run Code Online (Sandbox Code Playgroud)

c# optimization for-loop prefix trie

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