我正在尝试在注册用户时将用户添加到角色中,因此我将角色播种并使用以下代码在migrations.cs类中更新数据库
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
string[] roleNames = { "Admin", "Reviewer", "User" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
if (!RoleManager.RoleExists(roleName))
{
roleResult = RoleManager.Create(new IdentityRole(roleName));
}
}
Run Code Online (Sandbox Code Playgroud)
我试图将roleNames提取到我的accountcontroller类的下拉列表中
public ActionResult Register()
{
var model = new RegisterViewModel();
model.RolesList = new SelectList(_db.Roles, "Id", "Name");
return View(model);
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser()
{
UserName = model.UserName,
PortalUser = new PortalUser() …Run Code Online (Sandbox Code Playgroud) 我使用带有Identity v2的Asp.Net MVC5进行身份验证.我为用户创建编写的函数在开发过程中完美运行.但是,由于我几天前已经发布了生产,因此我有时会获得CreateAsync(user,pass)方法的异常.这是我的简化代码:
var store = new UserStore<SiteUser>(dc);
var userManager = new UserManager<SiteUser>(store);
var user = new SiteUser
{
UserName = email,
Email = email,
MemberID = member.MemberID
};
var createResult = await userManager.CreateAsync(user, rawPassword);
if (!createResult.Succeeded)
throw new Exception("User Creation Failed - Identity Exception");
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么我有时会得到这个例外?换句话说,这种CreateAsync方法将返回的条件是什么!createResult.Succeeded?
将我的项目更新到最近发布的ASP.NET 5 beta8后,我发现IServiceCollection不再包含ConfigureIdentity和的定义ConfigureIdentityApplicationCookie.
所以以前编写的代码就像
services.ConfigureIdentity(o =>
{
o.Password.RequireUppercase = false;
o.Password.RequireNonLetterOrDigit = false;
});
services.ConfigureIdentityApplicationCookie(o => o.LoginPath = "/Admin/Users/Login");
Run Code Online (Sandbox Code Playgroud)
不能再编译了.
谷歌搜索没有结果,我想这是因为自beta8发布以来仅过了一天.
有没有人为此找到解决方法?如何在beta8中配置身份选项?
在离线尝试Azure移动应用程序时(在Visual Studio和使用SSL的本地IIS中),必须提供Azure的WEBSITE_AUTH_SIGNING_KEY的替代方案作为JWT令牌的SigningKey.
如何生成开发SigningKey?
我们拥有ASP.NET应用程序,该应用程序已经投入生产了4-5年。现在,我正在尝试使用Azure AD,ASP.NET Identity和OWIN框架实施新的身份验证。我们的应用程序用户还需要访问已经使用Azure支持SSO的云应用程序“ BOX”。因此,想法是将我们的应用程序添加到Azure AD并配置SSO。
注意:我们没有本地的Active Directory,我只想使用Azure AD进行Web应用程序身份验证。
和往常一样,在阅读了有关MSDN的大量文档后,我感到困惑,我应该使用哪种身份验证机制。
第一种方法-根据 此处的Microsoft文章,
我已使用Nuget配置了以下软件包
PM> Install-Package Microsoft.Owin.Security.OpenIdConnect
PM> Install-Package Microsoft.Owin.Security.Cookies
PM> Install-Package Microsoft.Owin.Host.SystemWeb
Run Code Online (Sandbox Code Playgroud)
然后,我在Azure AD中配置了应用程序,然后在ASP.Net应用程序中,在应用程序设置中配置了“ ida:ClientId”,“ ida:Tenant”和“ ida:PostLogoutRedirectUri”。然后在启动时调用以下代码 (并按照文章进行一些额外的步骤)
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
});
}
Run Code Online (Sandbox Code Playgroud)
第二种方法–基于VS 2013 Ultimate提供的模板
在此处,当您使用VS 2013创建新应用程序并使用“组织帐户”选项配置身份验证时,它将添加以下appsettings'ida :FederationMetadataLocation ',' ida:Realm '和' ida:AudienceUri ',然后添加代码以验证用户身份
问题
1>我不确定应该使用哪种方法,有什么区别。
2>根据此处的 Microsoft视频,如果您使用第二种方法创建应用程序,则Visual Studio将在Azure AD中为该租户创建应用程序。但是它不再创建了,这最近改变了吗?我们是否总是需要在Azure AD中手动创建应用程序?
EDIT1 …
我使用了将用户添加到角色的"AddToRole".但是,此更改无法立即应用,此用户必须注销并登录才能应用.
那么如何立即更新此更改?
谢谢!
在我的应用程序中,我想在布局页面(_LoginPartial)中显示用户配置文件图像.
在AspNet Identity会员资格中他们是一张AspNerUser桌子.我想自定义此AspNerUser表以维护图像字段.
然后在布局页面(_LoginPartial)视图中显示该图像.
我怎样才能做到这一点 ?真的很感激可以建议一种方法来做到这一点
编辑
我使用ADO.NET实体模型生成了我的DataBaseContext名称,数据库上下文名称是 ProjectNameEntities
然后我尝试在PMC上使用以下命令启用迁移
Enable-Migrations -ContextTypeName myProject.Models.ProjectNameEntities
但后来我得到了跟随错误
不支持从使用Database First或Model First创建的DbContext创建DbModelBuilder或编写EDMX.EDMX只能从不使用现有DbCompiledModel创建的Code First DbContext中获取.
这可能与edmx模型有关吗?
asp.net-mvc entity-framework edmx asp.net-mvc-5 asp.net-identity
我正在使用ASP.NET MVC 5和Identity 2与Entity Framework 6的系统.当用户登录时,我向该登录会话添加一些声明.我不想使用索赔表.
对于我的一个主张,我确实喜欢这样:
public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
//We add the display name so that the _LoginPartial can pick it up;
userIdentity.AddClaim(new Claim("DisplayName", FirstName + " " + LastName));
// Add custom user claims here
return userIdentity;
}
public virtual ICollection<UserInsurance> UserInsurances { get; set; }
public User() …Run Code Online (Sandbox Code Playgroud) asp.net-mvc login claims asp.net-identity asp.net-identity-2
请解释一下OpenIddict中Applications表的用法.我正在关注这个很棒的教程http://capesean.co.za/blog/asp-net-5-jwt-tokens/ 它完美无缺.我误解了为OpenIddict添加的Applications表的功能.
目前,我的前端(Angularjs)+后端(带有ASP.NET Core和OpenIddict的Web.API)在一台服务器上运行良好.前端接收令牌并在请求中使用它.后端生成令牌并对其进行评估.一切都很好,但Applications表没有任何记录.
谢谢.
我正在使用ASP.NET MVC 5 / Entity 6 frameworke和代码优先方法在c#中构建应用程序。我正在尝试将Id身份2中的属性从更改string为integer。
我遵循的文档似乎很简单。
然后,我使用以下命令创建迁移
Add-Migration InitialCreate
Run Code Online (Sandbox Code Playgroud)
由于某种原因,迁移文件中不包含AspNet表。但是,然后我使用以下命令进行了迁移
Update-Database
Run Code Online (Sandbox Code Playgroud)
迁移没有问题,播种机运行良好。
但是,当我运行应用程序时
我收到以下错误
异常详细信息:System.FormatException:输入字符串的格式不正确。
该错误突出显示了Startup.Auth.cs文件下面的第38行。
Line 36: regenerateIdentityCallback: (manager, user) =>
Line 37: user.GenerateUserIdentityAsync(manager),
Line 38: getUserIdCallback: (id) => (id.GetUserId<int>()))
Run Code Online (Sandbox Code Playgroud)
如何解决此错误?
asp.net-identity ×10
asp.net-mvc ×6
c# ×3
asp.net ×2
asp.net-core ×2
azure ×1
claims ×1
edmx ×1
jwt ×1
login ×1
openiddict ×1
owin ×1