c# - Getting a null return from a GetUserManager call when using AspNet.Identity -
i have attempted go through few examples of utilizing microsoft.aspnet.identity , microsoft.asp.identity.owin within webforms. have been able create database tables within current database (i choose keep both aspnet tables user, stores , roles manager in same database application) , have model, controller, view , app_start folders in place needed files.
the line having issues in code behind file forgetpassword in account folder: var manager = context.getowincontext().getusermanager();
here how have files set up: app_start: identityconfig.cs
using system; using system.security.claims; using system.threading.tasks; using microsoft.aspnet.identity; using microsoft.aspnet.identity.entityframework; using microsoft.aspnet.identity.owin; using microsoft.owin; using microsoft.owin.security; using fhsf_4_dev.models; namespace fhsf_4_dev { public class emailservice : iidentitymessageservice { public task sendasync(identitymessage message) { // plug in email service here send email. return task.fromresult(0); } } public class smsservice : iidentitymessageservice { public task sendasync(identitymessage message) { // plug in sms service here send text message. return task.fromresult(0); } } // configure application user manager used in application. usermanager defined in asp.net identity , used application. public class applicationusermanager : usermanager<applicationuser> { public applicationusermanager(iuserstore<applicationuser> store) : base(store) { } public static applicationusermanager create(identityfactoryoptions<applicationusermanager> options, iowincontext context) { var manager = new applicationusermanager(new userstore<applicationuser>(context.get<applicationdbcontext>())); // configure validation logic usernames manager.uservalidator = new uservalidator<applicationuser>(manager) { allowonlyalphanumericusernames = false, requireuniqueemail = true }; // configure validation logic passwords manager.passwordvalidator = new passwordvalidator { requiredlength = 6, requirenonletterordigit = true, requiredigit = true, requirelowercase = true, requireuppercase = true, }; // register 2 factor authentication providers. application uses phone , emails step of receiving code verifying user // can write own provider , plug in here. manager.registertwofactorprovider("phone code", new phonenumbertokenprovider<applicationuser> { messageformat = "your security code {0}" }); manager.registertwofactorprovider("email code", new emailtokenprovider<applicationuser> { subject = "security code", bodyformat = "your security code {0}" }); // configure user lockout defaults manager.userlockoutenabledbydefault = true; manager.defaultaccountlockouttimespan = timespan.fromminutes(5); manager.maxfailedaccessattemptsbeforelockout = 5; manager.emailservice = new emailservice(); manager.smsservice = new smsservice(); var dataprotectionprovider = options.dataprotectionprovider; if (dataprotectionprovider != null) { manager.usertokenprovider = new dataprotectortokenprovider<applicationuser>(dataprotectionprovider.create("asp.net identity")); } return manager; } } public class applicationsigninmanager : signinmanager<applicationuser, string> { public applicationsigninmanager(applicationusermanager usermanager, iauthenticationmanager authenticationmanager) : base(usermanager, authenticationmanager) { } public override task<claimsidentity> createuseridentityasync(applicationuser user) { return user.generateuseridentityasync((applicationusermanager)usermanager); } public static applicationsigninmanager create(identityfactoryoptions<applicationsigninmanager> options, iowincontext context) { return new applicationsigninmanager(context.getusermanager<applicationusermanager>(), context.authentication); } } }
app_start: startup.auth.cs
using system; using microsoft.aspnet.identity; using microsoft.aspnet.identity.entityframework; using microsoft.aspnet.identity.owin; using microsoft.owin; using microsoft.owin.security.cookies; using microsoft.owin.security.dataprotection; using microsoft.owin.security.google; using owin; using fhsf_4_dev.models; namespace fhsf_4_dev { public partial class app_start_startup { // more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?linkid=301883 public void configureauth(iappbuilder app) { // configure db context, user manager , signin manager use single instance per request app.createperowincontext(applicationdbcontext.create); app.createperowincontext<applicationusermanager>(applicationusermanager.create); app.createperowincontext<applicationsigninmanager>(applicationsigninmanager.create); // enable application use cookie store information signed in user // , use cookie temporarily store information user logging in third party login provider // configure sign in cookie app.usecookieauthentication(new cookieauthenticationoptions { authenticationtype = defaultauthenticationtypes.applicationcookie, loginpath = new pathstring("~/default.aspx"), provider = new cookieauthenticationprovider { onvalidateidentity = securitystampvalidator.onvalidateidentity<applicationusermanager, applicationuser>( validateinterval: timespan.fromminutes(30), regenerateidentity: (manager, user) => user.generateuseridentityasync(manager)) } }); // use cookie temporarily store information user logging in third party login provider app.useexternalsignincookie(defaultauthenticationtypes.externalcookie); // enables application temporarily store user information when verifying second factor in two-factor authentication process. app.usetwofactorsignincookie(defaultauthenticationtypes.twofactorcookie, timespan.fromminutes(5)); // enables application remember second login verification factor such phone or email. // once check option, second step of verification during login process remembered on device logged in from. // similar rememberme option when log in. app.usetwofactorrememberbrowsercookie(defaultauthenticationtypes.twofactorrememberbrowsercookie); // uncomment following lines enable logging in third party login providers //app.usemicrosoftaccountauthentication( // clientid: "", // clientsecret: ""); //app.usetwitterauthentication( // consumerkey: "", // consumersecret: ""); //app.usefacebookauthentication( // appid: "", // appsecret: ""); //app.usegoogleauthentication(new googleoauth2authenticationoptions() //{ // clientid = "", // clientsecret = "" //}); } } }
account: forgotpassword.aspx
<%@ page title="forgot password" language="c#" masterpagefile="~/site.master" autoeventwireup="true" codebehind="forgotpassword.aspx.cs" inherits="fhsf_4_dev.account.forgotpassword" async="true" %> <asp:content runat="server" id="bodycontent" contentplaceholderid="maincontent"> <div style="width: 20%; text-align: center; color: white; margin-left: 40%; margin-right: 40%;"> <div class="row"> <div class="col-md-8"> <asp:placeholder id="loginform" runat="server"> <div class="form-horizontal"> <h2>forgot password?</h2> <hr /> <asp:placeholder runat="server" id="errormessage" visible="false"> <p class="text-danger"> <asp:literal runat="server" id="failuretext" /> </p> </asp:placeholder> <div class="form-group" style="align-content:center"> <asp:label runat="server" associatedcontrolid="email" cssclass="col-md-2 control-label">email</asp:label> <div class="col-md-10" style="margin-left:28%; margin-right:28%;"> <asp:textbox runat="server" id="email" cssclass="form-control" textmode="email" /> <asp:requiredfieldvalidator runat="server" controltovalidate="email" cssclass="text-danger" errormessage="the email field required." /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <asp:button runat="server" onclick="forgot" text="email link" cssclass="btn btn-default" /> </div> </div> </div> </asp:placeholder> <asp:placeholder runat="server" id="displayemail" visible="false"> <p class="text-info"> please check email reset password. </p> </asp:placeholder> </div> </div> </div> </asp:content>
account: forgotpassword.aspx.cs
using system; using system.web; using system.web.ui; using microsoft.aspnet.identity; using microsoft.aspnet.identity.owin; using owin; using fhsf_4_dev.models; using sendgrid; using system.net; using system.configuration; using system.diagnostics; using fhsf_4_dev; namespace fhsf_4_dev.account { public partial class forgotpassword : page { protected void page_load(object sender, eventargs e) { } protected void forgot(object sender, eventargs e) { if (isvalid) { // validate user's email address var manager = context.getowincontext().getusermanager<applicationusermanager>(); applicationuser user = manager.findbyname(email.text); if (user == null || !manager.isemailconfirmed(user.id)) { failuretext.text = "the user either not exist or not confirmed."; errormessage.visible = true; return; } // more information on how enable account confirmation , password reset please visit https://go.microsoft.com/fwlink/?linkid=320771 // send email code , redirect reset password page string code = manager.generatepasswordresettoken(user.id); string callbackurl = identityhelper.getresetpasswordredirecturl(code, request); manager.sendemail(user.id, "reset password", "please reset password clicking <a href=\"" + callbackurl + "\">here</a>."); loginform.visible = false; displayemail.visible = true; } } } }
it seems have things set needed, return var manager = context.getowincontext().getusermanager(); line in forgotpassword.aspx.cs null, though have user set in aspnetuser table.
any on leading me why getting null value , how can correct reference appreciated!
Comments
Post a Comment