我被赋予修改ASP.NET MVC应用程序的任务,以冲浪到myurl?username = xxxxxx将自动登录用户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.
解决方法
你只需要使用usermanager来查找用户的名字.如果你有一个记录,那么只需要登录.
public ActionResult StupidCompanyLogin() { return View(); } [HttpPost] //[ValidateAntiForgeryToken] - Whats the point? F**k security public async Task<ActionResult> StupidCompanyLogin(string name) { var user = await UserManager.FindByNameAsync(name); if (user != null) { await SignInManager.SignInAsync(user,true,true); } return View(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。