1

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.

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.

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
coderwill
  • 804
  • 3
  • 16
  • 38
  • Membership should have a method for logging in - be aware that ASP.NET identity replaces the membership approach: https://www.asp.net/identity – Brian Mains Feb 27 '17 at 13:12
  • @Brian mains i have try for the login Membership.ValidateUsers() method but i am always getting false – coderwill Feb 27 '17 at 13:27
  • Internally, it calls a stored procedure - you could try calling that asp.net stored procedure with your results, and see if it matches... – Brian Mains Feb 27 '17 at 14:22
  • A further suggestion to @Brian's comment. Run SQL Profiler too to see the execution of the stored proc and the results being sent back. It may give you an insight to what is happening too. – Judge Bread Feb 27 '17 at 15:19
  • 1
    If you're using MVC 5, look into ASP.NET Identity. Membership is deprecated. – Chris Pratt Feb 27 '17 at 15:33

1 Answers1

0

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.

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

In my 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");

}

I hope this helps.

Link / More Info: https://azure.microsoft.com/en-us/services/active-directory/external-identities/b2c/

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73