Click here to Skip to main content
15,886,362 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Blazor Pin
Vincent Maverick Durano21-May-18 16:43
professionalVincent Maverick Durano21-May-18 16:43 
GeneralRe: Blazor Pin
Super Lloyd22-May-18 3:20
Super Lloyd22-May-18 3:20 
QuestionAsp.net Pin
Member 138185619-May-18 2:52
Member 138185619-May-18 2:52 
AnswerRe: Asp.net Pin
Richard MacCutchan9-May-18 3:35
mveRichard MacCutchan9-May-18 3:35 
AnswerRe: Asp.net Pin
ZurdoDev14-Jun-18 10:51
professionalZurdoDev14-Jun-18 10:51 
QuestionLooking for an example of food and drink on a handheld device ? Pin
Member 24584676-May-18 16:11
Member 24584676-May-18 16:11 
AnswerRe: Looking for an example of food and drink on a handheld device ? Pin
Richard MacCutchan6-May-18 21:54
mveRichard MacCutchan6-May-18 21:54 
QuestionClaimsIdentity with null referance Pin
Member 138120214-May-18 7:28
Member 138120214-May-18 7:28 
Hello, new here. I am having some difficulty with claims Identity. The new Item that i have put in the "Add custom user claims here" could be a null or unanswered field during registration. It seems that it does not like that. I am getting a "Object reference not set to an instance of an object". This has no issue when the field in the database has something in it. Below is my code in the IdentityModel.
C#
public string HomeTown { get; set; }
        public DateTime? BirthDate { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            userIdentity.AddClaim(new Claim("BirthDate", BirthDate.ToString()));
            userIdentity.AddClaim(new Claim("HomeTown", HomeTown.ToString()));
            return userIdentity;
        }

I have also implemented a IdentityExtensions.cs so that i can pull a GetHomeTown.
I am trying to show HomeTown on the _loginPartial of the MVC. I have tried several flavors of "if" statements. So i am trying to show a actionlink if the there is not a HomeTown in the system and if there is show it.
Here is my code for _loginPartial:
C#
@{
    var bday = (User.Identity.GetBirthDate());
    var d = Convert.ToDateTime(bday).Date.ToString("d");
    var htown = (User.Identity.GetHomeTown());
    if (string.IsNullOrEmpty(htown))
    {
       <li> @Html.ActionLink("Update Info", "Update", "Home")</li>
    }
    else
    {
        <li>From @Html.Raw(htown)  </li>
    }
}


        <li>@Html.Raw(d)</li>

Here is the code from the IdentityExtensions:
C#
public static string GetHomeTown(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("HomeTown");
            // Test for null to avoid issues during local testing
            return (claim != null) ? claim.Value : string.Empty;
        }

I have no issues displaying GetHomeTown when there is something in the field. My "if" statements seem to never work. Some that i have tried will show whatever i put after the "if" statement and just ignore the else. Others do not work at all.

Is there something i am not getting here? Should i be doing this in the AccountController instead and where would i put this if i did?
Maybe in here:
C#
public AccountController()
{
    context = new ApplicationDbContext();
}

Thanks in advance for your help. Let me know if there is anything else that you need to see.

Update 12:08 EST 5/5/2018:
I was able to get around this by inserting a default value - html string. This way i do not need the "if" statement and it displays what i need with a link. When the user updates this with a Home Town it will delete the string and show their hometown. I think this is better anyhow.

I did however find out that my code does work if you clear the NULL from the SQL record field to an empty field. I tried putting a default value of ("") but SQL treats it as NULL in the field.

modified 5-May-18 0:15am.

QuestionASP.NET MVC - Display all date/times in a specific time zone Pin
ZurdoDev3-May-18 8:14
professionalZurdoDev3-May-18 8:14 
AnswerRe: ASP.NET MVC - Display all date/times in a specific time zone Pin
Richard Deeming3-May-18 8:55
mveRichard Deeming3-May-18 8:55 
QuestionASP.NET web application for DVR integration Pin
Milind Panchal3-May-18 1:12
Milind Panchal3-May-18 1:12 
Questionerror:AjaxControlToolkit.RatingExtender missing required StarCssClass property Pin
Member 138022222-May-18 5:20
Member 138022222-May-18 5:20 
AnswerRe: error:AjaxControlToolkit.RatingExtender missing required StarCssClass property Pin
Richard Deeming2-May-18 8:43
mveRichard Deeming2-May-18 8:43 
PraiseRe: error:AjaxControlToolkit.RatingExtender missing required StarCssClass property Pin
Member 138022222-May-18 10:37
Member 138022222-May-18 10:37 
QuestionWhat technique can be used for printing ALL PAGES from crystal report?(asp.net,vb.net) Pin
Member 126093771-May-18 23:21
Member 126093771-May-18 23:21 
QuestionAm using entity framework of doing CRUD operation there for i take index .cshtml to show list of employees am getting stuck to get it please give a suggestions. Pin
Ankit Bopche29-Apr-18 23:33
Ankit Bopche29-Apr-18 23:33 
AnswerRe: I have recently taken over support of an ASP.NET MVC project, and am trying to work through some of the errors, one in particular has me stumped though. Pin
F-ES Sitecore30-Apr-18 0:15
professionalF-ES Sitecore30-Apr-18 0:15 
Questionam a beginner who want to learn ASP.net, how do I start Pin
Member 1380311528-Apr-18 17:17
Member 1380311528-Apr-18 17:17 
AnswerRe: am a beginner who want to learn ASP.net, how do I start Pin
Afzaal Ahmad Zeeshan29-Apr-18 2:10
professionalAfzaal Ahmad Zeeshan29-Apr-18 2:10 
GeneralMessage Closed Pin
4-May-18 23:19
hirasetup4-May-18 23:19 
JokeRe: am a beginner who want to learn ASP.net, how do I start Pin
Afzaal Ahmad Zeeshan5-May-18 2:22
professionalAfzaal Ahmad Zeeshan5-May-18 2:22 
GeneralRe: am a beginner who want to learn ASP.net, how do I start Pin
Richard Deeming9-May-18 8:39
mveRichard Deeming9-May-18 8:39 
QuestionPrinting Pin
Member 1360728318-Apr-18 18:17
Member 1360728318-Apr-18 18:17 
AnswerRe: Printing Pin
Richard MacCutchan18-Apr-18 21:59
mveRichard MacCutchan18-Apr-18 21:59 
GeneralRe: Printing Pin
Member 1360728319-Apr-18 3:27
Member 1360728319-Apr-18 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.