根据这篇文章http://www.asp.net/web-api/overview/security/external-authentication-services ...我能够使用本地身份验证服务登录(使用新的Asp.net身份框架)
但无法找到一个walktrough来正确调用(从移动应用程序或邮递员)Visual Studio 2013 SPA模板中生成的默认Web api.
谁能帮我?
我创建了一个新的空MVC项目,我想为它添加标识.现在我不知道如何做到这一点以及如何创建数据库和表和类.我搜索了但是我没有找到任何有用的解决方案来解决我的问题,我找到了一篇文章,用于将ASP.NET身份添加到现有的空ASP.NET网站,但对于MVC我没有找到的ASP.Net 项目.
用于登录
注册表单,
用于将用户添加到角色
以删除用户
如何在我自己的数据库中创建表
如何管理用户
我正在使用Unity for Dependencies Injection并使用Identiy Provider来管理用户登录,注册,电子邮件确认等.
当我尝试注册用户时,我遇到了这个问题:
当前类型Microsoft.Owin.Security.IAuthenticationManager是一个接口,无法构造.你错过了类型映射吗?
我不知道如何在我的Unity容器中注册此接口(IAuthenticationManager).
我尝试使用此代码注册接口,但如果我把它,我还有其他问题:
没有注册IUserTokenProvider.
container.RegisterType<HttpContextBase>(
new InjectionFactory(_ => new HttpContextWrapper(HttpContext.Current)));
container.RegisterType<IOwinContext>(new InjectionFactory(c => c.Resolve<HttpContextBase>().GetOwinContext()));
container.RegisterType<IAuthenticationManager>(
new InjectionFactory(c => c.Resolve<IOwinContext>().Authentication));
Run Code Online (Sandbox Code Playgroud)
我把一些应用程序的代码(如果我不使用Unity,一切正常):
的AccountController
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
Run Code Online (Sandbox Code Playgroud)
IdentityConfig.cs
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail …Run Code Online (Sandbox Code Playgroud) 我创建了一个UserModel实现的ViewModel()IUser<int>(用于自定义ASP.NET Identity 2.0)
public class UserModel : IUser<int>
{
public int Id { get; set; }
public string SecurityStamp { get; set; }
[Display(Name = "Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public string FullName { get; set; }
[Display(Name = "Username")]
public string UserName { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "E-Mail")]
public …Run Code Online (Sandbox Code Playgroud) 有谁知道如何让用户通过电子邮件确认更改带有ASP.NET身份的用户名/电子邮件?有很多关于如何更改密码的例子,但我找不到任何关于此的信息.
道歉并提前感谢您提出这个问题!我还是SO的新手.
我一直在使用MVC5,EF6和VS 2013开发Web应用程序.
一旦发布,我花了一些时间升级到RC位.感谢所有伟大的帖子:例如.解耦Microsoft.AspNet.Identity.*并将asp.net MVC从5.0.0-beta2更新到5.0.0-rc1!
在我无限的智慧中,我决定转向@Hao Kung在这里发布的RTM位:我如何能够及早访问即将到来的Asp.Net Identity变化?.我认为当我们最终收到RTM版本时,我会省去麻烦并且不会太落后.
这可能是一场噩梦,或者我只是完全遗漏了某些东西(或两者兼而有之),因为我无法弄清楚曾经使用RC1的基本任务.
虽然我似乎是通过控制器登录用户(Asp.Net Identity RTM版本中的Microsoft.AspNet.Identity.Owin.AuthenticationManager在哪里?)...我的WindowsIdentity始终为空,在我调用SignIn后未经过身份验证.正确填充了user和claimIdentity对象.
这是我调用的操作方法(为了完整性将属性移动到局部变量):
[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public virtual async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid) {
var userManager = new UserManager<EtdsUser>(new UserStore<EtdsUser>(new EtdsContext()));
var user = userManager.Find(model.UserName, model.Password);
if (user != null) {
var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut(new[] {DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ExternalBearer});
var claimsIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe}, claimsIdentity);
return RedirectToLocal(returnUrl);
}
}
ModelState.AddModelError("", "The …Run Code Online (Sandbox Code Playgroud) 我正在探索SignInManager类.但MSDN上提供的信息非常无用.它只说明提供的方法和属性.
我要找的是,
1)什么是SignInManager?2)如何使用它?3)我有自己的数据库,其中包含凭据相关信息(用户名和密码)
我如何使用SignInmanager以及如何使用它,以便我的自定义数据库用于验证用户?
我使用的是asp.net MVC 5和Visual Studio 2015.在我的示例项目中,我有一个包含操作方法的帐户控制器
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用它,MSDN完全没用提供这方面的信息.任何有用的链接都会详细解释它,因为我不知道SignInManager是什么以及它的用途.
谢谢
(使用VS2013 RTW,ASP.NET MVC5)
我在使用ASP.NET标识时看到了很多关于如何向ApplicationUser类(和表)添加属性的文档.但是我没有看到任何关于如何通过外键将内容映射到ApplicationUser表的单独表格的文档.
我已经成功地派生了我自己的Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext,现在我自己的"UserPreferences"表与我的SQL Server数据库中的各种"AspNet*"表共存.但我不清楚获取当前用户ID的最佳方法,以便在"UserPreferences"表中写入新行.请注意,该项目是ASP.NET MVC,但我已添加(并且正在使用)Web API控制器.
我有一个有效的解决方案,它是:
var uman = new Microsoft.AspNet.Identity.UserManager<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(new App1.Data.App1DbContext()));
var uident = User.Identity;
var userobject = uman.FindByNameAsync(uident.Name);
var userid = userobject.Result.Id;
// (Perform new row creation including the userid we just looked up
Run Code Online (Sandbox Code Playgroud)
考虑到AspNetUsers表(由Identity框架定义)包含以下字段:
-id (PK, nvarchar(128) - seems to contain a GUID, not sure why not an autoincrement integer, but I assume there are reasons for this)
-Username (nvarchar(max))
-PasswordHash (nvarchar(max))
-SecurityStamp (nvarchar(max))
Run Code Online (Sandbox Code Playgroud)
我认为id字段(不是用户名)是此表中引用的正确字段.(我当时认为用户可能会更改他/她的用户名,然后其他人就可以假设其他用户的用户名,这就是为什么两个字段都可能存在的原因.)那么,我需要获取当前用户的存储空间ID. "UserPreferences"表,这是上面的代码所做的.但是,进行此查找似乎效率低下.
重要的一点是,在System.Web.Mvc.Controller的上下文中,我可以这样做:
User.Identity.GetUserId()
(Runtime type of User.Identity: System.Security.Principal.GenericIdentity) …Run Code Online (Sandbox Code Playgroud) asp.net-mvc asp.net-web-api owin visual-studio-2013 asp.net-identity
我将在本文后面从Identity 1.0.0迁移到Identity 2.0.1
生成的迁移代码与新的IdentityUser无关.它不会添加新列.
所以我创建了一个新项目并再次尝试,但迁移代码为空.
为了解决这个问题,我直接在SQL Server中进行了编辑,并在我的解决方案中再次导入了我的数据库.
现在我和你所看到的AspNetUser完全一样IdentityUser
IdentityUser
public virtual int AccessFailedCount { get; set; }
public virtual ICollection<TClaim> Claims { get; }
public virtual string Email { get; set; }
public virtual bool EmailConfirmed { get; set; }
public virtual TKey Id { get; set; }
public virtual bool LockoutEnabled { get; set; }
public virtual DateTime? LockoutEndDateUtc { get; set; }
public virtual ICollection<TLogin> Logins { get; }
public virtual string PasswordHash …Run Code Online (Sandbox Code Playgroud) 我已经使用ASP.net会员多年,我刚刚开始尝试ASP.net身份.他们刚刚发布了2.0版,它应该支持int主键.我已经定义了我的自定义标识类,以获得整数主键支持:
public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>
{
}
public class UserLogin : IdentityUserLogin<int>
{
}
public class Role : IdentityRole<int, UserRole>
{
public string Description { get; set; } // Custom description field on roles
}
public class UserRole : IdentityUserRole<int>
{
}
public class UserClaim : IdentityUserClaim<int>
{
}
public class MyDbContext : IdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>
{
public MyDbContext() : this("MyDB") { }
public MyDbContext(string connStringName) : base(connStringName)
{
this.Configuration.LazyLoadingEnabled = …Run Code Online (Sandbox Code Playgroud)