When someone logs on to my site. I want to direct them to their own home page. If the user has the id of 1. They would go to
http://www.test.com/Home.aspx?id=1
I already have the login and id setup. I am not sure how to incorporate it into the url.
Response.Redirect("http://www.test.com/Home.aspx?id=" + id);
Are you using Forms Authentication?
If so, instead of using RedirectFromLoginPage (which will redirect to whatever page is in your web.config), just use FormsAuthentication.SetAuthCookie, and do your own redirection.
To do so, you need to make use of the URL QueryString.
E.g
// forms auth code here, user is logged in.
int id = 1;
string redirectUrlFormat = "http://www.test.com/Home.aspx{0}";
string queryStringidFormat = "?id={0}";
Response.Redirect(string.Format(redirectUrlFormat, string.Format(queryStringidFormat, id)));
You should handle all querystring parameters, URL, etc (ie the above code) in a global static model class.
That way you can just say:
Response.Redirect(SomeStaticClass.GetUserHomePageUrl(id));
In the receiving page (Home.aspx), use the following code to get the id of the user:
var userId = Request.QueryString["id"]; // again, this "magic string" should be in a static class.
Hope that helps.
Related
I am sending link to email address for password reset functionality and after sometime i want this link to expire. for that i have created a token(which is encrytped using a key) and expire-date and i want to put these as query in my email link but i don't know to do it.
this is how i use token class in forgotPassword Post method.
var tokenModel = new LinkExpire();
tokenModel.ExpiresOn = DateTime.Now.AddSeconds(10);
tokenModel.CreateToken = TokenHelperMethods.GetToken(tokenModel);
this is my link code.
string resetCode = Guid.NewGuid().ToString();
var varifyUrl = "/E_HealthCare_Web/Account/ResetPassword/" + resetCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, varifyUrl);
and in my email body i am sending link like this
"<br/> <br/> <a href = '" + link +"&expire="+tokenModel.ExpiresOn+"&token="+tokenModel.CreateToken+"'>Reset link</a> <br/><br/>" +
which does not to work as expected. anyone can help me achieve this, also i am not using core, only mvc5.
Edit this is my controller where i am recieving link values
public ActionResult ResetPassword(string id, DateTime expire, string token)
while clicking on link gives A potentially dangerous Request.Path value was detected from the client (&) error.
Here is my suggestion, instead of adding expiration token query parameters with URL manage this at your method action level i.e.
You already have the information that which login is going to this URL. All you have to do is that before sending this URL via email, make a separate temp table that will have user ID, reset password URL path, created date/time column (this column will mange the data/time when you send the URL to the user for password reset) and active/Iactive status column.
Now at code level when this particular URL is hit by user, first get the active row only entry against this URL & user ID and get the created date/time column value.
Check the difference between the active created date/time column and current date/time.
if difference between two dates is more than 24hr send expiration response otherwise change the password.
Mark that entry as inactive.
Know that against each user the active entry in this new table exist only when user request's password reset, otherwise all existing entries are marked as inactive.
You can delete instead of active/inactive as well. this is temp table.
I have a web Api in C#. I need user id/name in the api controller function when ever the user tries to access the api.
I tried Request.Principle and Http.Context.User but in both the identity.Name is comming as null. Can you please tell how can I get the username in the Api.
string _userName = System.Web.HttpContext.Current.User.Identity.Name;
Try this one.
You can use the following in you class to get the user Id
var userId = System.Web.HttpContext.Current.User.Identity.GetUserId();
I dont do aspx at all so trying to work out this simple task.
I see the following code in some cs files which guess gets the current user and i assume this is a standard method in asp but might be wrong:
CS:
User user = (User)Context.Items["CurrentUser"];
I have tried things like this from other posts on here but maybe this system is different or the setup is different? again i dont know.
var currentUser = Membership.GetUser(User.Identity.Name);
string username = currentUser.UserName; //** get UserName
Guid userID = currentUser.ProviderUserKey; //** get user ID
Does anyone know how i can get the Name and User ID of the current user based on what is written above?
it depends on how you handle users in your website.
if you use the asp.net built in user management, then User.Identity.Name will get you the currently logged in username.
other stuff like (User)Context.Items["CurrentUser"] or (User)Session["myUser"] will get you the user which was saved in those places somewhere in your website.
you just need to start your way from the login page, and follow the functions to see how users are being handled in your website.
There are lots of sample applications in MVC but the current project I'm working on requires that I use web forms.
I can authorize the application using the javascript method but I want to use server side. Below is what I started with on the page.load
dynamic parameters = new ExpandoObject();
parameters.client_id = AppId;
parameters.client_secret = appSecret;
parameters.response_type = "code";
//parameters.state = state;
parameters.redirect_uri = "http://fb.local/page.aspx";
// The requested response: an access token (token), an authorization code (code), or both (code token).
parameters.response_type = "token";
// list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
//parameters.display = "popup";
// add the 'scope' parameter only if we have extendedPermissions.
if (!string.IsNullOrWhiteSpace(ExtendedPermissions))
parameters.scope = ExtendedPermissions;
// generate the login url
var fb = new FacebookClient();
var loginUrl = fb.GetLoginUrl(parameters);
Response.Redirect(loginUrl.AbsoluteUri, true);
I can authorize but I'm not able to get the access token from the URL.
On the next page I can view source and see the access token in the url bar but I'm not sure how to go about getting it into the code. once I have the token, I'm all set.
page.aspx#access_token=AAACrxQhmdpY
I used to this code on my page load and works, its not a very clean code, but you may figure out how to change it for your best use. so the algorithm is that when the page loads you redirect the user to Facebook authentication page using response.redirect to a string that contains three parameters:your app ID(appid), what permissions you are asking your user(scope), where you want Facebook to redirect the user after authorization, and a parameter as state which i guess it should be a random number. so after the user authorized your application he/she will be redirected to your page, with a request URL that contains the same state you prepared Facebook with(and you can use to identify who which request was which if there are many requests i guess) and also a new "code" parameter which you pass on to Facebook to obtain access token, you can use Facebook c# sdk to obtain the access token.in my code there is a line that says "if code is not null, go to alireza" and alireza is a line tag after the response.redirect code, this is because you dont want the process to be repeated all over and over (and of course probably the browser show an error).
int intstate;
string strstate;
string redirecturltofb;
string scope;
string appid;
code = Request.QueryString["code"];
if (!String.IsNullOrWhiteSpace(code))
{
goto alireza;
}
appid = "424047057656831";
scope = "user_about_me,user_activities,user_groups,email,publish_stream,user_birthday";
intstate = 45;
strstate = Convert.ToString(intstate);
redirecturltofb = "https://www.facebook.com/dialog/oauth?client_id=" + appid + "&redirect_uri=http://test5-2.apphb.com/&scope=" + scope + "&state=" + strstate;
Response.Redirect(redirecturltofb);
You have to use Javascript SDK to get access token back to code behind.
Use FB.Init as in http://csharpsdk.org/docs/web/getting-started
and do post back on certain conditions to get the access token.
Thank you,
Dharmendra
How can I redirect www.mysite.com/picture/12345 to www.mysite.com/picture/some-picture-title/12345? Right now, "/picture/12345" is rewritten on picture.aspx?picid=12345 and same for second form of url (picture/picture-title/12323 to picture.aspx?picid12323) I can't just rewrite first form of url to second because i have to fetch picture title from database.
On the first hand, problem looks very easy but having in mind time to parse every request, what would be the right thing to do with it?
Not knowing what is the ASP.NET technology (Webforms or MVC) I will assume it's WebForms.
You can have a look at URL Redirecting and build you own rules. And you do this one time only to apply to all of the links that look like you want.
Scott Guthrie has a very nice post about it.
If what you want is to when it comes to that address redirect to a new one, it's quite easy as well.
First of all let's reuse the code, so you will redirect first to a commum page called, for example, redirectme.aspx
in that page you get the REFERER address using the ServerVariables or passing the Url in a QueryString, it's your chooise and then you can attach the title name, like:
private void Redirect()
{
// get url: www.mysite.com/picture/12345
string refererUrl = Request.ServerVariables["HTTP_REFERER"]; // using the ServerVariables or Request.UrlReferrer.AbsolutePath;
//string refererUrl = Request.QueryString["url"]; // if you are redirecting as Response.Redirect("redirectme.aspx?" + Request.Url.Query);
// split the URL by '/'
string[] url = refererUrl.Split('/');
// get the postID
string topicID = url[url.Length-1];
// get the title from the post
string postTitle = GetPostTitle(topicID);
// redirect to: www.mysite.com/picture/some-picture-title/12345
Response.Redirect(
String.Format("{0}/{1}/{2}",
refererUrl.Substring(0, refererUrl.Length - topicID.Length),
postTitle,
topicID));
}
to save time on the server do this on the first Page event
protected void Page_PreInit(object sender, EventArgs e)
{
Redirect();
}
If you're running IIS7 then (for both webforms and MVC) the URL rewriting module (http://learn.iis.net/page.aspx/460/using-url-rewrite-module/) is worth looking at.
Supports pattern matching and regular expressions for redirects, state whether they're temporary or permanent, plus they're all managable through the console. Why code when you don't have to?
I'm presuming you need a common pattern here and not just a once off solution. i.e. you'll need it to work for 12345 & 12346 & whatever other IDs as well. You're probably looking for URLRedirection and applying a Regular Expression to identify a source URI that will redirect to your Target URI