如何在MVC 5中创建具有成员资格的新用户 [英] How do you create a new user with Membership in MVC 5

查看:73
本文介绍了如何在MVC 5中创建具有成员资格的新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用mvc c#在angularjs中创建了一个小型演示,我想使用会员资格提供程序提供登录功能.我已经尝试过使用此简单的代码从数据库中获取记录,但是它没有从数据库中返回任何用户.数据库中有用户.

I have created a small demo in angularjs with mvc c# and I want to have a login facility using membership provider. I have tried this simple code for the getting records from the database but it doesn't return any users from the database. There are users in the database.

web.config文件中的成员资格设置:

The membership setting in web.config file:

<membership defaultProvider="AspNetSqlMembershipProvider">
  <providers>
    <clear />
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="50" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

然后我调用此方法来获取所有用户:

then I call this method to get all users:

Membership.GetAllUsers()

有人知道这个问题可能是什么吗?谢谢.

Does anyone know what the problem might be? Thanks.

推荐答案

如果您有Azure订阅,则建议使用Azure AD B2C.这使您可以专门为您的应用程序创建一个Azure AD实例.它们支持Azure AD帐户,您可以配置其他身份提供程序,例如Google,Facebook,MS Personal Account(outlook.com),Linkedin等.

If you have an Azure subscription, then I would recommend using Azure AD B2C. This allows you to create an Azure AD instance specifically for your app(s). They support Azure AD accounts and you can configure additional identity providers like Google, Facebook, MS Personal Account (outlook.com), Linkedin, etc.

它允许您在门户中定义定制概要文件属性,并允许您配置新用户注册",编辑我的概要文件",密码重置"等的布局/体验.

It allows you to define custom profile properties in the portal and enabled you to configure the layout / experience for 'New User SignUp', 'Edit My Profile', 'Password Reset', and others.

如果在应用程序中使用Azure AD B2C和ASP.NET Identity,则无需自己注册用户.但是,如果您希望在注册后访问新用户的数据或声明,则应该可以通过修改AccountController.cs

If you're using Azure AD B2C and ASP.NET Identity in your application, you do not need to sign-up a user yourself. However, if you want access to a new user's data or claims after sign-up, you should be able to access it by modifying the AccountController.cs

在我的AccountController.cs中:

public void SignUpSignIn(string redirectUrl = "")
{
    // this redirect url is where the user is routed after authentication.
    var default_redirectUrl = "/account/gateway"; 
    if (string.IsNullOrWhiteSpace(redirectUrl))
    {
        redirectUrl = default_redirectUrl;
    }
    // Use the default policy to process the sign up / sign in flow
    HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = redirectUrl });
    return;
}

// this is where the user is redirected after sign-up
public async Task<ActionResult> Gateway()
{
    // if you cast the User.Identity as 
    // System.Security.Claims.ClaimsIdentity then you can access 
    // the claims from the identity provider.
    var userClaims = (User.Identity as ClaimsIdentity).Claims;

    var issuer_claim = userClaims.FindClaim("iss")?.Value;
    var nameid_claim = userClaims.FindClaim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
    var audience_claim = userClaims.FindClaim("aud")?.Value;
    var auth_time_claim = userClaims.FindClaim("auth_time")?.Value;
    var idprovider_claim = userClaims.FindClaim("http://schemas.microsoft.com/identity/claims/identityprovider")?.Value;
    var displayname_claim = userClaims.FindClaim("name")?.Value;
    var objectid_claim = userClaims.FindClaim("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
    var country_claim = userClaims.FindClaim("country")?.Value;
    var newUser_claim = userClaims.FindClaim("newUser")?.Value;
    var emails_claim = userClaims.FindClaim("emails")?.Value;

    // Then you can use this claims as needed. 

    // after this navigate to your homepage or whatever makes sense.
    return RedirectToAction("Index", "Home");

}

我希望这会有所帮助.

链接/更多信息: https://azure.microsoft.com/zh-CN/services/active-directory/external-identities/b2c/

这篇关于如何在MVC 5中创建具有成员资格的新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆