标签: owin

使用OWIN标识从多个API客户端注册Web API 2外部登录

我想要以下架构(我已经为这个例子编写了产品名称):

在一台服务器 http://api.prettypictures.com 上运行的Web API 2应用程序

MVC 5客户端应用程序在另一台服务器上运行 http://www.webpics.com

我希望www.webpics.com客户端应用程序使用Pretty Pictures API:

  • 使用用户名和密码注册新帐户
  • 在Facebook/Google/Twitter/Microsoft注册新帐户
  • 登录
  • 检索图片

所有上述工作除了在Facebook,Google等注册外部账户外.

我无法确定从API的单独客户端用户创建外部帐户的正确流程.

我已经研究了认证流程中可用的大多数文档,如下所示: 在此输入图像描述

我已经在OWIN的新身份模型上阅读了我所能做的一切.

我已经在Visual Studio 2013中检查了SPA模板.它演示了如何完成我需要的大部分工作,但仅限于客户端和API在同一主机上时; 如果我希望多个客户端访问我的API并且能够让用户通过Google等注册,那么它就无法正常工作,而且我可以告诉OWIN身份验证流程中断.

到目前为止流程如下:

  • 用户浏览www.webpics.com/Login
  • www.webpics.com调用api.prettypictures.com/Account/ExternalLogins(用RETURNURL设置回去在回调www.webpics.com),并显示最终链接到用户
  • 用户点击"Google"
  • 浏览器使用提供者的名称重定向到api.prettypictures.com/Account/ExternalLogin等.
  • API的ExternalLogin操作实例化对google.com的挑战
  • 浏览器被重定向到google.com
  • 用户输入用户名和密码(如果他们尚未登录google.com)
  • google.com现在提供安全许可:"api.prettypictures.com"想要访问您的电子邮件地址,姓名,妻子,孩子等.这样可以吗?
  • 用户点击"是"并使用Google设置的Cookie将其带回api.prettypictures.com/Account/ExternalLogin.

这是我被卡住的地方.接下来应该发生的是以某种方式通知客户端应用程序用户已成功通过google.com进行身份验证,并获得一次性访问代码以便稍后交换访问令牌.如有必要,客户端应用程序应该有机会提示用户输入与google.com登录相关联的用户名.

我不知道如何促进这一点.

实际上,此时浏览器在谷歌回调后最终坐在api.prettypictures.com/Account/ExternalLogin端点上.该API已登录Google,但客户端不知道如何处理该问题.我应该将该饼干送回www.webpics.com吗?

在SPA应用程序中,它通过AJAX完成,google.com将返回一个令牌作为URL片段,它一切运行良好,因为它都在一个域上.但这远远超过了拥有多个客户可以充分利用的"API"的重要性.

救命!

asp.net-mvc oauth-2.0 owin asp.net-identity asp.net-web-api2

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

重命名ASP.NET MVC项目时出错

我复制了以前的项目并将其重命名.一旦我成功重命名所有名称空间并正确构建.运行应用程序时出现以下错误:

The following errors occurred while attempting to load the app.
- The OwinStartup attribute discovered in assembly 'User Manager Interface' referencing    startup type 'User_Manager_Interface.Startup' conflicts with the attribute in assembly   'Service Monitor Web Interface' referencing startup type  'Service_Monitor_Web_Interface.Startup' because they have the same FriendlyName ''. Remove or   rename one of the attributes, or reference the desired type directly.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-authentication owin asp.net-mvc-5

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

找不到OWIN HttpListener

当我尝试开始时:

WebApp.Start<SrvcHst>(new StartOptions { Port = 9956, 
     ServerFactory = "Microsoft.Owin.Host.HttpListener" });
Run Code Online (Sandbox Code Playgroud)

我得到以下异常.可能是根本原因?

System.MissingMemberException was caught
  HResult=-2146233070
  Message=The server factory could not be located for the given input: Microsoft.Owin.Host.HttpListener
  Source=Microsoft.Owin.Hosting
  StackTrace:
       at Microsoft.Owin.Hosting.Engine.HostingEngine.ResolveServerFactory(StartContext context)
       at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
       at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)
       at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
       at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
Run Code Online (Sandbox Code Playgroud)

c# exception .net-4.5 owin asp.net-web-api2

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

无法从apicontroller中的OwinContext获取UserManager

我正在关注Microsoft示例以使用Identity 2.0.0实现电子邮件验证

我被困在这一部分

public ApplicationUserManager UserManager
{
   get
   {
      return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
   }
   private set
   {
      _userManager = value;
   }
}
Run Code Online (Sandbox Code Playgroud)

这适用于controllerHttpContext不包含ApiController中的任何GetOwinContext方法.

所以我试过HttpContext.Current.GetOwinContext()但方法GetUserManager不存在.

我无法找到一种方法来获得UserManager我在Startup.Auth.cs中的构建

// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
    //Configure the db context, user manager and role manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
    ...
}
Run Code Online (Sandbox Code Playgroud)

这条线

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)

调用以下函数来配置 UserManager …

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

68
推荐指数
3
解决办法
7万
查看次数

Owin Twitter登录 - 根据验证程序,远程证书无效

我最近尝试使用twitter登录时遇到此错误 - 任何想法为什么?

Stack Trace: 


[AuthenticationException: The remote certificate is invalid according to the validation procedure.]
   System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) +230
   System.Net.PooledStream.EndWrite(IAsyncResult asyncResult) +13
   System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) +123

[WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.]
   System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +6432446
   System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) +64
Run Code Online (Sandbox Code Playgroud)

.net twitter asp.net-mvc twitter-oauth owin

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

OWIN的GetExternalLoginInfoAsync始终返回null

我创建了一个新的MVC5 Web应用程序,当我尝试使用Google或Facebook登录时,调用了ExternalLoginCallbackAction AccountController,但GetExternalLoginInfoAsync()始终返回null:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
    return RedirectToAction("Login");
}
Run Code Online (Sandbox Code Playgroud)

因为它总是为null,所以它只是重定向回登录页面并且过程重新开始.我怎样才能解决这个问题?

.net c# asp.net-mvc google-openid owin

67
推荐指数
5
解决办法
2万
查看次数

找不到包含OwinStartupAttribute错误的程序集

这个错误

尝试加载应用程序时发生以下错误. - 找不到包含OwinStartupAttribute的程序集. - 找不到给定的类型或方法'false'.尝试指定程序集.要禁用OWIN启动发现,请在web.config中添加值为"false"的appSetting owin:AutomaticAppStartup.要指定OWIN启动程序集,类或方法,请在web.config中添加appSetting owin:AppStartup以及完全限定的启动类或配置方法名称.

出现在我的屏幕上,在历史上创建的最丑陋的丑陋错误页面上.

在此输入图像描述

我试图通过在配置中插入owin:AutomaticAppStartup来遵循页面上的说明.

 <appSettings >
    <add key="owin:AppStartup" value="false"></add>
        </appSettings>
Run Code Online (Sandbox Code Playgroud)

这没有解决问题.有什么建议?

c# web-config .net-assembly owin

67
推荐指数
6
解决办法
10万
查看次数

如何在Microsoft.AspNet.Identity.EntityFramework.IdentityUser中更改id的类型

(ASP.NET MVC 5,EF6,VS2013)

我试图弄清楚如何在类型中将"Id"字段的类型从字符串更改为int:

Microsoft.AspNet.Identity.EntityFramework.IdentityUser
Run Code Online (Sandbox Code Playgroud)

为了使新用户帐户与整数ID而不是GUID相关联.但似乎这比在我的派生用户类中简单地添加类型为int的新Id属性更复杂.看看这个方法签名:

(来自程序集Microsoft.AspNet.Identity.Core.dll)

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
  {
  ...
  public virtual Task<IdentityResult> AddLoginAsync(string userId, UserLoginInfo login);
  ...
  }
Run Code Online (Sandbox Code Playgroud)

因此,似乎ASP.NET身份框架中还有其他方法要求userId为字符串.我还需要重新实现这些课程吗?

解释为什么我不想在用户表中存储id的GUID:

- 将有其他表通过外键将数据与users表相关联.(当用户在网站上保存内容时.)我认为没有理由使用较大的字段类型并花费额外的数据库空间而没有明显的优势.(我知道还有其他关于使用GUID和int ID的帖子,但似乎有很多人建议int id更快并且使用更少的空间,这仍然让我感到疑惑.)

- 我计划公开一个restful端点,以允许用户检索有关特定用户的数据.我认为:

/users/123/name
Run Code Online (Sandbox Code Playgroud)

比...更干净

/users/{af54c891-69ba-4ddf-8cb6-00d368e58d77}/name
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么ASP.NET团队决定以这种方式实现ID?在尝试将其更改为int类型时,我是否只是短视?(也许我缺少一些好处.)

谢谢...

-ben

asp.net-mvc entity-framework owin asp.net-mvc-5 asp.net-identity

66
推荐指数
3
解决办法
4万
查看次数

没有OWIN身份验证管理器与请求关联

在尝试为我的Web Api项目启用owin和AspNet Identity(在VS 2013 + .Net 4.5.1中)后,我在每个有效或无效(请求到无存在的控制器)请求中收到以下错误:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
No OWIN authentication manager is associated with the request.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.SuppressDefaultAuthenticationChallenges(HttpRequestMessage request) at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
</StackTrace>
</Error>
Run Code Online (Sandbox Code Playgroud)

当我在调试模式下检查时,也没有处理任何异常!我也意识到ConfigurationStartup课堂上永远不会被调用(实际上从未被调试器捕获过).这是启动代码:

[assembly: OwinStartup(typeof(bloob.bloob.Startup))]

namespace bloob.bloob
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

windows-authentication owin asp.net-mvc-5

66
推荐指数
5
解决办法
4万
查看次数

ASP.NET MVC5 OWIN Facebook身份验证突然无法正常工作

2017年更新!

我在发布原始问题时遇到的问题与Facebook最近在强迫每个人使用其API版本2.3时所做的更改无关.有关该特定问题的解决方案,请参阅下面的sammy34的答案./ oauth/access_token端点的2.3版现在返回JSON而不是表单编码的值

由于历史原因,这是我原来的问题/问题:

我有一个MVC5 Web应用程序,它使用内置支持通过Facebook和Google进行身份验证.几个月前我们构建这个应用程序时,我们遵循了这个教程:http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and -google-oauth2-and-openid-sign-on,一切都很棒.

现在,突然之间,Facebook身份验证刚刚停止工作.Google身份验证仍然有效.

问题描述:我们点击链接使用Facebook连接,我们被重定向到Facebook,如果我们不允许我们的Facebook应用访问我们的个人资料,我们会被提示.当我们点击"确定"时,我们会被重定向回我们的网站,但我们只是登录屏幕而不是登录.

我已经在调试模式下完成了这个过程,并且根据上面提到的教程,我在我的帐户控制器中有了这个ActionResult:

// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (loginInfo == null)
    {
        return RedirectToAction("Login");
    }
    ............
Run Code Online (Sandbox Code Playgroud)

当单步执行代码并从Facebook返回时,loginInfo对象始终为NULL,这会导致用户重定向回登录.

为了理解幕后实际发生的事情,我安装了Fiddler并监控HTTP流量.我发现的是,在Facebook权限对话框中单击"确定"后,Facebook会使用以下URL重定向回我们的应用程序:

https://localhost/signin-facebook?code=<access-token>
Run Code Online (Sandbox Code Playgroud)

这个URL不是一个实际的文件,可能是由我猜测的这个OWIN框架内置的一些控制器/处理程序处理的.最有可能的是,它使用给定的代码连接回Facebook,以查询有关尝试登录的用户的信息.现在,问题是我们被重定向到:而不是这样做:

/Account/ExternalLoginCallback?error=access_denied
Run Code Online (Sandbox Code Playgroud)

我确信这是Facebook正在做的事情,也就是说,它不是向我们提供用户数据,而是通过此错误消息重定向我们.

这会导致AuthenticationManager.GetExternalLoginInfoAsync();失败并始终返回NULL.

我完全没有想法.据我们所知,我们没有改变任何事情.

我已经尝试创建一个新的Facebook应用程序,我已经尝试过再次使用该教程,但我总是遇到同样的问题.

欢迎任何想法!

更新!

好的,这让我疯了!我现在已经手动完成了执行身份验证所需的步骤,当我这样做时,一切都很顺利.为什么在使用MVC5 Owin的东西时这不起作用?

这就是我做的:

    // Step 1 - Pasted this into a browser, this returns a code
    https://www.facebook.com/dialog/oauth?response_type=code&client_id=619359858118523&redirect_uri=https%3A%2F%2Flocalhost%2Fsignin-facebook&scope=&state=u9R1m4iRI6Td4yACEgO99ETQw9NAos06bZWilJxJrXRn1rh4KEQhfuEVAq52UPnUif-lEHgayyWrsrdlW6t3ghLD8iFGX5S2iUBHotyTqCCQ9lx2Nl091pHPIw1N0JV23sc4wYfOs2YU5smyw9MGhcEuinvTAEql2QhBowR62FfU6PY4lA6m8pD3odI5MwBYOMor3eMLu2qnpEk0GekbtTVWgQnKnH6t1UcC6KcNXYY

I was redirected back to localhost (which I had shut …
Run Code Online (Sandbox Code Playgroud)

facebook owin asp.net-mvc-5

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