ASP.NET MVC Identity login without password

梦想与她 提交于 2019-11-27 12:53:11

问题


I have been given the assignment of modifying an ASP.NET MVC application in such a way that surfing to myurl?username=xxxxxx would automaticly log in user xxxxxx, without asking for passwords.

I already made it very clear that this is a terrible idea for many security related reasons and scenarios, but the people in charge are determined. The site would not be publicly available.

So: is there any way of signing in without password by for example extending the Microsoft.AspNet.Identity.UserManager and modifying the AccountController?

Some code:

  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 holds an instance of a Microsoft.AspNet.Identity.UserManager.

and 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 would be OwinSecurity.


回答1:


You just need to use the usermanager to find the user by name. If you have a record then just sign them in.

    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();
    }


来源:https://stackoverflow.com/questions/28110934/asp-net-mvc-identity-login-without-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!