locked
is it possible to adjust Identity validation rules for single method? RRS feed

  • Question

  • User-1305530094 posted

    I have default setup for my Identity at startup. can I change certain rules at a method level,

    i.e I want to be able to create a user without email just for the method below?

      private async Task<IdentityResult> CreateOnlineAccount (ApplicationUser user) {
    
                IdentityResult result = await this.userManager.CreateAsync (user);
                return result;
            }

    Saturday, April 21, 2018 5:35 AM

Answers

  • User475983607 posted

    The email address is not a required field by default and you have total control over which fields are saved.   The only way the email is saved is if you have an email field in a View, use the field to populate the ApplicationUser email property, and invoke userManager,CreateAsync.

    Can you explain the problem you are having? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 21, 2018 10:52 AM
  • User-1305530094 posted
    this.repository.AssignAccountOnly (patientUser);
    
    repository.AddPatientToRegistry (patient);
    
    await unitOfWork.CompleteAsync ();


    So I figured out that `CreateAsync` method will run `validateAsync` auto and validate according to my setup. 

    my workaround, which suites my case here, is just to insert a record directly using dbContext,

    this is in my repository, and I call it in my controller after injecting the interface,

    public void AssignAccountOnly (ApplicationUser user) {
                context.ApplicationUser.Add (user);
            } 

    My controller 

    this.repository.AssignAccountOnly (patientUser);
    repository.AddPatientToRegistry (patient);
    await unitOfWork.CompleteAsync ();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 22, 2018 6:29 AM

All replies

  • User475983607 posted

    The email address is not a required field by default and you have total control over which fields are saved.   The only way the email is saved is if you have an email field in a View, use the field to populate the ApplicationUser email property, and invoke userManager,CreateAsync.

    Can you explain the problem you are having? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 21, 2018 10:52 AM
  • User-1305530094 posted

    this is my setup at <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">starup</g>

    services.AddIdentity<ApplicationUser, IdentityRole>
                    (options => {
    options.User.RequireUniqueEmail = bool.Parse (Configuration["IdentityOptions:User:RequireUniqueEmail"]);
    });
    
    

    when I try to provide null email to my API <g class="gr_ gr_8 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace" id="8" data-gr-id="8">i</g> get this error,

    warn: Microsoft.AspNetCore.Identity.UserManager[13]
    User b42759d5-8085-4d35-b309-c82111bf4f50 validation failed: InvalidUserName;InvalidEmail.

    I know that my email is getting validated, how can I work around it? I want the global setup to validate the email, I just want to disable it for a single menthod

    Sunday, April 22, 2018 3:23 AM
  • User-1305530094 posted
    this.repository.AssignAccountOnly (patientUser);
    
    repository.AddPatientToRegistry (patient);
    
    await unitOfWork.CompleteAsync ();


    So I figured out that `CreateAsync` method will run `validateAsync` auto and validate according to my setup. 

    my workaround, which suites my case here, is just to insert a record directly using dbContext,

    this is in my repository, and I call it in my controller after injecting the interface,

    public void AssignAccountOnly (ApplicationUser user) {
                context.ApplicationUser.Add (user);
            } 

    My controller 

    this.repository.AssignAccountOnly (patientUser);
    repository.AddPatientToRegistry (patient);
    await unitOfWork.CompleteAsync ();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 22, 2018 6:29 AM