locked
Get OwinContext problems RRS feed

  • Question

  • User-605499000 posted

    Using Owin Identity programs and I am having problems with three items ApplicationUserManager, PhoneNumber and IdentifyHelper.  The file is a verifyphone number aspx.cs file. The addphonenumber file is fine. Below is the code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.Owin;

    namespace Bmples.Account
    {
    public partial class VerifyPhoneNumber : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
    var phonenumber = Request.QueryString["PhoneNumber"];
    var code = manager.GenerateChangePhoneNumberToken(User.Identity.GetUserId(), phonenumber);
    PhoneNumber.Value = phonenumber;
    }
    protected void Code_Click(object sender, EventArgs e)
    {
    if (!ModelState.IsValid)
    {
    ModelState.AddModelError("", "Invalid code");
    return;
    }

    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();

    var result = manager.ChangePhoneNumber(User.Identity.GetUserId(), PhoneNumber.Value, Code.Text);

    if (result.Succeeded)
    {
    var user = manager.FindById(User.Identity.GetUserId());

    if (user != null)
    {
    IdentityHelper.SignIn(manager, user, false);
    Response.Redirect("/Account/Manage?m=AddPhoneNumberSuccess");
    }
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "Failed to verify phone");
    }
    }
    }

    I don't understand why I am having so many Owin Identy problems. I have solved the rest but can't get this one/  The error says that I am missing a directive or assembly. I copied the file from the Microsoft Wingtop program and there were no problems.

    Thank, Jen

    Friday, October 19, 2018 4:00 PM

Answers

  • User-605499000 posted

    Thank you for all of your help. I am not a programmer but trying to get my site back online and appreciate all the help I am getting.

    Jen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 12:57 AM

All replies

  • User753101303 posted

    Hi,

    You are using a "standard" template ? Try maybe https://forums.asp.net/t/1981988.aspx?ASP+NET+Identity+2+0+Where+is+ApplicationUserManager+

    Generally speaking I would suggest to use VS Object Explorer. You can search a class and find in which namespace it is defined (or use "search in all files" ?). If it fails do that in the app from which you copied this to see if you missed copying some source code or if maybe you used a DLL ?

    Friday, October 19, 2018 4:40 PM
  • User475983607 posted

    It's very helpful when the actual error is posted with the line that caused the error.  Otherwise we have to look through the code, which is not formatted by the way, and guess.

    Please post the error message and line of code that causes the error.  For reference, there is an Insert/Edit Code button in the Message toolbar.  It looks like this...

    {;}

    Use this feature when posting code as it makes it easier to read.

    One this I see with the code there's redundancy.  The Page_Load event has local UserManager and there's page class UserManager.  I recommend creating basic web project that uses the standard Web Forms with Individual Account option.  Use this project as a template that shows how to use Identity. 

    Friday, October 19, 2018 4:57 PM
  • User-605499000 posted

    Thank you I am sorry that I did not show you the mistake and also did not know there was a special thing to put in next to the mistake. I also do not see the duplicate. I do see that I should not have added everything after the helpers.

    Here is the paragraph with the mistake.

    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);  (It is in this line Create Identity) 
    // Add custom user claims here
    return userIdentity;
    }

    I did read a lot about the applications user managers and was able to fix other parts of this programmer but could not do it with this one.

    I am not a programmer just trying to update my old site and I was using Microsoft wingtop and even with changes the errors have popped up.

    Thanks Jen

    Friday, October 19, 2018 6:04 PM
  • User-605499000 posted

    I have fixed the create identity but I have another problem in identity mode.  Here is the problem.

     public static implicit operator ApplicationDbContext(Bumples.ApplicationDbContext v)  (*ApplicationDBContext  has the error Does not exist in the name space am I missing a directive or assembly)
    {
    throw new NotImplementedException();
    }

    I had added 2 statements in Identity conf..cs which I took out and the create identity  above was fixed.  Below is what I took out of identity config

    public class ApplicationUser : IdentityUser

    {

    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

    return userIdentity;

    }

    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    {

    public ApplicationDbContext()

    : base("DefaultConnection", throwIfV1Schema: false)

    {

    }

    public static ApplicationDbContext Create()

    {

    return new ApplicationDbContext();

    }

    }

    There was someone else on this thread that I have lost that sent me back to identity config to check. I have lost his name.

    Thanks for your help and the other person.

    Jen

    Saturday, October 20, 2018 2:00 AM
  • User-605499000 posted

    Thank you for all of your help. I am not a programmer but trying to get my site back online and appreciate all the help I am getting.

    Jen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 12:57 AM