- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想知道这里是否有更高效的路线。使用 AspNet.Identity
我想允许用户使用他们的 UserName
或 Email
登录到同一个文本框。我继续在 AccountController Login ActionResult
中解决了这个问题。我在调用之前运行检查:
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true);
支票:
//TODO: determine if there is a more efficient way to allow user to login either with Email || UserName
if (model.UserName.Contains("@"))
{
using (var context = new ApplicationDbContext())
{
model.UserName = (context.Users.Any(p => p.Email == model.UserName)) ?
context.Users.SingleOrDefault(p => p.Email == model.UserName).UserName :
model.UserName;
}
}
我的担忧有两方面:
I am including the entire
ActionResult
below for reference.
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
//TODO: determine if there is a more efficient way to allow user to login either with Email || UserName
if (model.UserName.Contains("@"))
{
using (var context = new ApplicationDbContext())
{
model.UserName = (context.Users.Any(p => p.Email == model.UserName)) ?
context.Users.SingleOrDefault(p => p.Email == model.UserName).UserName :
model.UserName;
}
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
最佳答案
会有一个安全问题。如果你知道他的电子邮件,你可以得到另一个用户的用户名:
我会声明新变量而不是重用 model.UserName。如果您使用 FirstOrDefault,您的查询会更有效:
var userName = model.UserName;
using (var context = new ApplicationDbContext())
{
var user = context.Users.FirstOrDefault(p => p.Email == model.UserName);
if (user != null)
{
userName = user.UserName;
}
}
var result = await SignInManager.PasswordSignInAsync(userName, model.Password, model.RememberMe, shouldLockout: true);
关于c# - 允许用户使用电子邮件或用户名 (AspNet.Identity) 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009221/
我将 aspnetboilerplate/aspnetzero 模板用于具有多数据库的 Multi-Tenancy SaaS 应用程序。这使用 CaSTLeWindsor 作为 DI 框架。 我遇到了
在我的应用程序中,我安装了以下两个 nuget 包: Microsoft.AspNet.Cors - 5.2.2 Microsoft.AspNet.WebApi.Cors - 5.2.2 我的应用程序
我有一个 aspnet core web api 项目,可以通过发布向导将其部署到 Azure 应用服务。但后来我在普通的 aspnet web api(.Net Framework 4.6 上的 M
2015 年 11 月 20 日更新 如何针对使用旧成员(member)提供程序存储用户的数据库进行身份验证? (aspnet_* 表)知道这个数据库被其他应用程序使用,所以我没有迁移(或更改)数据库
我正在使用最新的 VS2015 社区和 ASP.NET 5 等构建 AngularJS 单页应用程序... 我现在在尝试实现身份验证时遇到的问题是我需要使用这两个命名空间: Microsoft.Asp
这两个库有什么区别: https://www.nuget.org/packages/Microsoft.AspNet.Web.Optimization/1.1.0 https://www.nuget.
如果我使用安装的 Nuget 包 Microsoft.AspNet.FriendlyUrls v 1.0.2 和 Microsoft.AspNet.Identity v.1.0.0. 从 jQuery
我们在 ASP.NET 5 中进行身份验证时遇到了麻烦。In this security sample ,我们看到了这种东西: app.Run(async context => { va
背景:我们正在进行的项目包含多个共享两个库的解决方案。今天一切都是用 .NET Framework 4.6.1 编写的。该项目的目标是为新项目采用 .NET Core 并能够在 Docker 中运行
我正在使用带有 OData 端点的 Web API 和 Entity Framework 创建一个 RESTful 服务。 Microsoft.AspNet.WebApi.OData 和 Micros
在 VS 2015 中,升级 NuGet 包后,我收到以下警告: Dependency specified was Microsoft.AspNet.Mvc >= 6.0.0-beta6 but en
作为一枚后端程序狗,项目实践常遇到定时任务的工作,最容易想到的的思路就是利用Windows计划任务/wndows service程序/Crontab程序等主机方法在主机上部署定时任务程序/脚本。
研究身份提供者的概念证明,需要帮助理解 aspnet 身份的一些细微差别,特别是与用户声明相关的。 我想要完成的事情: 1) 公开一个 MVC 应用程序,该应用程序提供对 2 个微服务之一的安全访问,
我正在尝试使用一些非常标准的算法来解密字符串。 public static string DecryptString(string cipherText) { string keyString
我知道这取决于项目,但我想了解典型的 asp.net 核心项目是否有通用做法(例如忽略 node_modules)。 最佳答案 截至 2020 年,您应该使用 dotnet new gitignore
1. 加入dll文件这是必须的。 2.拖入控件到应用位置,添加引用: 引用: <%@ Register Assembly="AspNetPage
为什么 mvc 5 中包含身份验证过滤器? mvc 5 中的身份验证过滤器和授权过滤器之间的主要区别是什么? 最佳答案 我找到了以下博文:ASP.NET MVC 5 Authentication Fi
我正在尝试遵循一些过时的 AspNet Core 1.1 教程项目 (虽然我使用的是 Visual Studio 2019 预览版 2.0, 和 .net core v3.0.0-preview9 安
如果我使用本地 Web 服务器进行开发,我可以在不安装 IIS 的情况下从命令行使用实用程序 aspnet_compiler 编译网站项目 (3.5) 吗? 谢谢 最佳答案 是的你可以。您必须指定将在
我有一个.NET Core 2 Web应用程序,并且我想使用ASP.NET Identity来验证我的用户。在.NET Core 1.x上,我的代码运行正常。 我迁移到.NET Core 2,并且在V
我是一名优秀的程序员,十分优秀!