首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用短信密码登录ASP.Net标识2-不使用双因素身份验证

使用短信密码登录ASP.Net标识2-不使用双因素身份验证
EN

Stack Overflow用户
提问于 2016-04-15 09:11:55
回答 1查看 2.5K关注 0票数 2

下面是@Taiseer (http://bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/)的博客系列文章,我使用JWT身份验证构建了简单的WebApi。我可以使用令牌登录和查询我的端点。

我试图改变我的登录机制。

现在,用户必须将他的登录名和密码发送到/oauth/token,以获得将与每个请求一起发送到webapi的访问令牌。

我希望能够登录用户的密码,将通过短信发送到他的帐户分配的电话号码。

它应该需要两个对令牌端点路径的请求。首先只有用户id (登录或某些唯一标识符),然后端点应该检查用户是否存在,如果是,则应该通过SMS发送密码。

然后,用户将输入我的表单中的密码,第二个请求将使用用户id和密码对令牌端点路径执行。

我读过关于双因素身份验证 (http://bitoftech.net/2014/10/15/two-factor-authentication-asp-net-web-api-angularjs-google-authenticator/和其他来源)的文章,但是它要求用户使用用户名和密码登录,然后从短信中提交第二个密码。

在我的例子中,我希望用户只传递用户名和短信密码。

编辑:下面是我尝试构建的代码,它是起点,但我不知道如何实现它。我已经超越了GrantCustomExtension in OAuthAuthorizationServerProvider

代码语言:javascript
复制
public override async Task GrantCustomExtension(OAuthGrantCustomExtensionContext context)
{
    const string allowedOrigin = "*";
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

    //custom grant_type
    if (context.GrantType != "sms")
    {
        context.SetError("invalid_grant", "unsupported grant_type");
        return;
    }

    var userName = context.Parameters.Get("username");
    if (userName == null)
    {
        context.SetError("invalid_grant", "username is required");
        return;
    }

    var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
    ApplicationUser user = await userManager.FindByNameAsync(userName);
    if (user == null)
    {
        context.SetError("invalid_grant", "user not found");
        return;
    }

    //generate one-time password
    //store it in database
    //send it to user phone number
    //return ok message

    context.Validated();

    //return base.GrantCustomExtension(context);
}

下面是负责设置Startup.cs的oAuthAuthorizationServer的一部分:

代码语言:javascript
复制
public void ConfigureOAuthTokenGeneration(IAppBuilder app)
{
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

    var issuer = ConfigurationManager.AppSettings["Issuer"];

    OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()
    {
        #if DEBUG
        AllowInsecureHttp = true,
        #endif
        TokenEndpointPath = new PathString("/oauth/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(1),
        Provider = new CustomOAuthProvider(),
        AccessTokenFormat = new CustomJwtFormat(issuer)
    };

    // OAuth 2.0 Bearer Access Token Generation
    app.UseOAuthAuthorizationServer(oAuthServerOptions);

}

我能够发送自定义grant_type并在数据库中找到用户,但最困难的部分仍然缺失。我知道我可以将这个一次性密码存储在用户表中,但是也许ASP.Net Identity 2有内置的机制,我不想重新发明轮子。

EN

回答 1

Stack Overflow用户

发布于 2016-04-15 09:23:17

您可以通过每次更改用户密码来做到这一点。

不知道在你的情况下是否被接受

如果您对此解决方案感兴趣,下面是如何做到这一点:

用户管理器

代码语言:javascript
复制
    ApplicationUserManager _userManager;

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

// get your user here
var currentUser = UserManager.Users...
            if (currentUser != null)
            {
                var resultRemove = UserManager.RemovePassword(currentUser.Id);
                var resultAdd = UserManager.AddPassword(currentUser.Id, model.NewPassword);
                if (resultAdd.Succeeded && resultRemove.Succeeded)
                    //Send SMS here
            }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36642938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档