Ash*_*old 5 c# asp.net entity-framework asp.net-web-api asp.net-identity
我和同学正在使用Web API作为我们的后端开发Phonegap应用程序.以前我们在MVC4模板上使用旧的成员函数有一个工作原型.
我们已经找到了一些非常好的教程,介绍如何在普通的mvc控制器上扩展IdentityUser.我们设法让它工作,但转到VS 2013 SPA模板附带的API帐户控制器,我们没有设法让它工作.为了简化我们的问题,这里基本上是我们想要做的.
假设我们有一个像这样的Movie类:
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ApplicationUser User { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经创建了这样的ApplicationUser(取自具有个人帐户的VS 2013 MVC模板):
public class ApplicationUser : IdentityUser
{
public string DisplayName { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Movie> Films { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在global.asax上的Application_Start中添加了这一行:
Database.SetInitializer<ApplicationDbContext>(new AppDbInit());
Run Code Online (Sandbox Code Playgroud)
AppDbInit类:
public class AppDbInit : DropCreateDatabaseAlways<ApplicationDbContext>
{
}
Run Code Online (Sandbox Code Playgroud)
我更改了Startup.Auth.cs中的UserManagerFactory:
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
public static Func<UserManager<ApplicationUser>> UserManagerFactory { get; set; }
Run Code Online (Sandbox Code Playgroud)
在AccountController.cs文件中,我用ApplicationUser替换了所有出现的IdentityUser.完成后,我开始通过使用IdentityUser类的Microsoft.AspNet.Identity.EntityFramework中的类文件获取错误,说明它无法将IdentityUser转换为ApplicationUser.其中一个实体框架类是:
namespace Microsoft.AspNet.Identity.EntityFramework
{
public class IdentityUserLogin
{
public IdentityUserLogin();
public virtual string LoginProvider { get; set; }
public virtual string ProviderKey { get; set; }
public virtual IdentityUser User { get; set; }
public virtual string UserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
}
我创建了自己的类ApplicationUserLogin,其中我将User属性更改为ApplicationUser类型之一,但也无效.
这就是我被困住的地方.我通过电子邮件发送并寻求帮助,并建议编写我自己的UserStore类来实现IUserStore接口.这是扩展asp.net身份创建的EF代码优先数据库还是我关闭的唯一方法?我或许可以将我的用户帐户保存在一个数据库中,其余的保存在另一个数据库中,但我更愿意让它工作.
哦,这是我一直在使用的文章(就像我说我设法在标准的mvc控制器上工作):http: //blogs.msdn.com/b/webdev/archive/2013/10/20 /building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx
Bre*_*een -1
请参阅 Microsoft 内部团队关于自定义应用程序用户的以下帖子:http ://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-身份-in-vs-2013-templates.aspx
| 归档时间: |
|
| 查看次数: |
13307 次 |
| 最近记录: |