我被分配修改一个ASP.NET MVC应用程序,使得访问myurl?username=xxxxxx
的用户可以自动登录,而无需输入密码。
我已经非常清楚地表明了这是一个很糟糕的想法,因为涉及到许多安全相关的原因和情况,但是负责人还是坚持要这么做。该站点不会对公众开放。
所以:是否有任何方法可以在不使用密码的情况下进行登录,例如通过扩展Microsoft.AspNet.Identity.UserManager
并修改AccountController
?
一些代码:
var user = await _userManager.FindAsync(model.UserName, model.Password);
if (user != null && IsAllowedToLoginIntoTheCurrentSite(user))
{
user = _genericRepository.LoadById<User>(user.Id);
if (user.Active)
{
await SignInAsync(user, model.RememberMe);
_userManager
保存了一个Microsoft.AspNet.Identity.UserManager
的实例。
还有SignInAsync()
:
private async Task SignInAsync(User user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
if (user.UserGroupId.IsSet())
user.UserGroup = await _userManager.Load<UserGroup>(user.UserGroupId);
//adding claims here ... //
AuthenticationManager.SignIn(
new AuthenticationProperties { IsPersistent = isPersistent },
new CustomClaimsIdentity(identity));
}
AuthenticationManager
将被替换为 OwinSecurity
。