salesforce owin oauth login返回未经授权的响应

时间:2017-09-07 14:08:12

标签: c# oauth salesforce owin

我目前正在尝试使用owin安全提供程序在我的网络应用上验证salesforce用户。

更新:通过将salesforceOptions.CallbackPath = new PathString("/callback");添加到Startup.Auth.cs文件,我可以导航到登录屏幕。但是,当我输入我的凭据时,我将返回登录页面。 function AuthenticationManager.GetExternalLoginInfo();返回null。

  

Salesforce设置(示例):
  消费者密钥= abc
  消费者秘密= def
  选定的OAuth范围=基本(身份,个人资料,电子邮件,地址电话)
  回调网址= http://localhost:12345/callback http://localhost:12345/account/externallogin
  启用设备流=假
  要求网络服务器的密码flow = true
  包括自定义属性=假
  包括自定义权限= false

     

创建解决方案:
  我在vs 2015中创建了一个带有个人用户身份验证的MVC应用程序   我添加了NugetPackage Owin.Security.Providers.Salesforce

// Startup.Auth.cs class
public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        // Generated code
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
       app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
       app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
       app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        // I added this
        var salesforceOptions = new SalesforceAuthenticationOptions
        {
            Endpoints =
               new SalesforceAuthenticationOptions.SalesforceAuthenticationEndpoints
               {
                   AuthorizationEndpoint =
                       "https://login.salesforce.com/services/oauth2/authorize",
                   TokenEndpoint = "https://login.salesforce.com/services/oauth2/token"
               },
            ClientId = "abc",
            ClientSecret = "def"
        };
        // I added the following to get one step further:
        salesforceOptions.CallbackPath =  new PathString("/callback");
        app.UseSalesforceAuthentication(salesforceOptions);
    }
}

不对web.config或任何其他文件进行任何更改。当我运行应用程序时,我收到了salesforce登录按钮。当我按下此按钮并输入我的凭据时,我将被重定向到“登录”页面,没有登录详细信息。

我希望返回我的LoginInfo的功能在帐户控制器中:
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

我可以丢失哪些设置?

1 个答案:

答案 0 :(得分:0)

未经授权的回复是red herring。它与此无关。我在谷歌登录期间收到了相同的401,但工作正常

我找到了问题的答案。我设法在日志记录中找到了一条消息,告诉我以下内容:

  

需要更强的安全性

     

要访问此网站,请更新您的网络浏览器或升级您的操作系统以支持TLS 1.1或TLS 1.2。

TSL =&gt; “传输层安全性。”

在Google上搜索后,我找到了this解决方案。通过在我的启动代码中添加以下行,我解决了我的问题:

using System.Net;

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
        // rest of Startup.Auth.cs code
    }
}

我这样做后我可以正常登录。