在MVC5我怎么能创造code用户记录? [英] In MVC5 how can I create user records in code?

查看:83
本文介绍了在MVC5我怎么能创造code用户记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VS2013,MVC5,VB

VS2013, MVC5, VB

该MVC5模板效果很好的应用程序运行时,从注册查看创建新用户。但是,如果我想创建code用户,我该怎么做呢?

The MVC5 template works well to create new users from the Register View while the application is running. But if I want to create users in code, how do I do that?

以下是天真code,但说明了什么行不通:

The following is naïve code, but demonstrates what doesn't work:

Dim user = New ApplicationUser() With {.UserName = "name@live.com", .Email = "name@live.com"}
Dim acct As New AccountController
acct.UserManager.CreateAsync(user, "Temp1.11")

这行不通,因为是没有的UserManager的HttpContext。

This doesn't work because there is no HttpContext for UserManager.

还有很多事情在这里,我不明白,我有点失落。我所要做的只是简单地创建使用Configuration.vb种子法的用户列表。我想我可能只是复制现有AccountController.Register方法​​的工作方式,但显然不是。

There's a lot going on here that I don't understand and I'm a little lost. What I set out to do was simply create a list of users using the seed method in Configuration.vb. I thought I could just copy the way the existing AccountController.Register method works, but obviously not.

在回答我的问题最终将如何在种子创建用户,但我想也明白了为什么我的想法真的错了大约只是想用code的部分从注册方法。我不太明白的HttpContext以及如何应运而生。

The answer to my question would ultimately be how to create users in the seed, but I'd like to also understand why my thinking was so wrong about simply trying to use portions of the code from the Register method. I don't quite understand the HttpContext and how that comes into being.

推荐答案

您不需要使用的AccountController 来获得访问的UserManager 对象。而不仅仅是创建直接:

You don't need to use AccountController to get access to the UserManager object. Instead just create that directly:

Dim user = New ApplicationUser() With {.UserName = "name@live.com", .Email = "name@live.com"}
Dim myUserManager As New UserManager()
myUserManager.CreateAsync(user, "Temp1.11")

这篇关于在MVC5我怎么能创造code用户记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆