加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET MVC身份登录无密码

发布时间:2020-12-15 06:32:44 所属栏目:百科 来源:网络整理
导读:我被赋予修改ASP.NET MVC应用程序的任务,以冲浪到myurl?username = xxxxxx将自动登录用户xxxxxx,而不需要密码. 我已经很清楚,这是一个可怕的想法,许多安全相关的原因和情景,但负责人是决心的.该网站不会公开. 所以:有没有任何方式登录没有密码,例如扩展Mic
我被赋予修改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();
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读