Add restrictions to custom policy and make claim o

2019-04-15 14:40发布

问题:

I am trying to collect user details using LocalAccountSignUpWithLogonName custom policy.I have added <OutputClaim ClaimTypeReferenceId="email" /> to LocalAccountSignUpWithLogonName as outputclaim. I want to make the email field optional, but if user enter the email I want to enable the restrictions.Below is my email claim

<ClaimType Id="email">
<DisplayName>Your Email Address</DisplayName>
<DataType>string</DataType>
<DefaultPartnerClaimTypes>
    <Protocol Name="OpenIdConnect" PartnerClaimType="email" />
</DefaultPartnerClaimTypes>
<UserHelpText>Email address that can be used to contact you.</UserHelpText>
<UserInputType>TextBox</UserInputType>
<Restriction>
    <Pattern RegularExpression="^[a-zA-Z0-9.+!#$%&amp;'^_{}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$" HelpText="Please enter a valid email address." />
</Restriction>

But when I add the pattern restriction to claim, its making the field mandatory.

回答1:

I achieved it by changing the regular expression from RegularExpression="^[a-zA-Z0-9.+!#$%&amp;'^_{}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$" to RegularExpression="^$|^[a-zA-Z0-9.+!#$%&amp;'^_{}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$" `.

I had to prefix the expression with ^$| which accept blank/empty or the actual email.