Archives

Archives / 2014 / April
  • Form Validation Formatting in ASP.NET MVC 5 and Bootstrap 3

    When creating a new MVC project in ASP.NET MVC 5, Bootstrap is already included. For some reason proper formatting for form errors (the red colored error message and the red border around the controls) are not working. There are loads of articles and blog posts how to change this and that to enable this, but in ASP.NET MVC 5, the only thing you actually have to do is add a few classes to your Site.css file. Why they aren’t in there from the outset I don’t know.

    Site.css

    /* styles for validation helpers */
    .field-validation-error {
    color: #b94a48;
    }

    .field-validation-valid {
    display: none;
    }

    input.input-validation-error {
    border: 1px solid #b94a48;
    }


    select.input-validation-error {
    border: 1px solid #b94a48;
    }

    input
    [type="checkbox"].input-validation-error {
    border: 0 none;
    }

    .validation-summary-errors {
    color: #b94a48;
    }

    .validation-summary-valid {
    display: none;
    }

    Sample form.cshtml

    @model WebApplication6.Models.TestModel

    @{
    ViewBag.Title = "Home Page";
    }
    <br /><br />

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
    <div class="form-group">
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
    @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
    <br />
    @Html.ValidationMessageFor(m => m.Name)
    </div>
    </div>
    <div class="form-group">
    @Html.LabelFor(m => m.GenderId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
    @Html.DropDownListFor(m => m.GenderId, new SelectList(Model.Genders, Model.GenderId), "", new { @class = "form-control" })
    <br />
    @Html.ValidationMessageFor(m => m.GenderId)
    </div>
    </div>


    <div class="form-group">
    <div class="col-md-offset-2 col-md-10">
    <input type="submit" class="btn btn-default" value="OK" />
    </div>
    </div>
    }


    Sample TestModel.cs

    public class TestModel
    {
    [Required(ErrorMessage = "Name is required")]
    [MinLength(3, ErrorMessage = "Name must be at least 3 letter")]
    public string Name { get; set; }
    [Display(Name= "Gender")]
    [Required(ErrorMessage = "Gender is required")]
    public string GenderId { get; set; }

    public string[] Genders = new[] {"Male", "Female"};
    }


  • Subsequent calls to AcquireToken()

    Using the Active Directory Authentication Library (ADAL) and getting that annoying flash from the authentication dialog on subsequent calls? Maybe you’re creating a new AuthenticationContext every time? In that case the call to AcquireToke() by the context cannot keep and lookup the cached token and refreshtokens. You should try to keep a cached or static version of the AuthenticationContext alive between calls. Something like this of you’re calling a web api or similar:

    private static readonly AuthenticationContext AuthenticationContext = new AuthenticationContext("https://login.windows.net/domain.com");

    private static AuthenticationResult GetAuthenticationToken()
    {
    var acquireToken = AuthenticationContext.AcquireToken("https://domain.com/WebApp-something.azurewebsites.net", //resource id of the web api
    "acca2f90-5f76-45b5-8ec3------", //the client id
    new Uri("https://domain.com/YourClientRedirectUrl")); //client redirect url

    return acquireToken;
    }

    public static async Task<string> GetRequestAsync(string requestUri)
    {
    var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
    var authorizationHeader = GetAuthenticationToken().CreateAuthorizationHeader();
    request.Headers.TryAddWithoutValidation("Authorization", authorizationHeader);
    var response = await client.SendAsync(request);
    var responseString = await response.Content.ReadAsStringAsync();
    Debug.WriteLine(responseString);
    return responseString;
    }